From 313b2a61e2d6cb6c5ee82fbba3d22cc1d0cb08b6 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann <2310924+florianbrinkmann@users.noreply.github.com> Date: Thu, 5 Dec 2024 21:30:24 +0100 Subject: [PATCH] [5.x] Add validation replacements to replicator and grid field types (#10255) Co-authored-by: Florian Brinkmann Co-authored-by: Jason Varga --- .../AddsEntryValidationReplacements.php | 32 +++++++++++++++++++ src/Fieldtypes/Grid.php | 7 +++- src/Fieldtypes/Replicator.php | 7 +++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/Fieldtypes/AddsEntryValidationReplacements.php diff --git a/src/Fieldtypes/AddsEntryValidationReplacements.php b/src/Fieldtypes/AddsEntryValidationReplacements.php new file mode 100644 index 0000000000..3fbd97d938 --- /dev/null +++ b/src/Fieldtypes/AddsEntryValidationReplacements.php @@ -0,0 +1,32 @@ +parent(); + + if (! $fieldParent instanceof Entry) { + return $rules; + } + + return $rules->withReplacements([ + 'id' => $fieldParent->id(), + 'collection' => $fieldParent->collection()->handle(), + 'site' => $fieldParent->locale(), + ]); + } +} diff --git a/src/Fieldtypes/Grid.php b/src/Fieldtypes/Grid.php index 964f860fe0..c9783322d9 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -14,6 +14,8 @@ class Grid extends Fieldtype { + use AddsEntryValidationReplacements; + protected $categories = ['structured']; protected $defaultable = false; @@ -158,7 +160,10 @@ protected function rowRules($data, $index) ->validator() ->withContext([ 'prefix' => $this->field->validationContext('prefix').$this->rowRuleFieldPrefix($index).'.', - ]) + ]); + + $rules = $this + ->addEntryValidationReplacements($this->field, $rules) ->rules(); return collect($rules)->mapWithKeys(function ($rules, $handle) use ($index) { diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index 117ec1f5ee..ee6e1a5722 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -16,6 +16,8 @@ class Replicator extends Fieldtype { + use AddsEntryValidationReplacements; + protected $categories = ['structured']; protected $keywords = ['builder', 'page builder', 'content']; protected $rules = ['array']; @@ -150,7 +152,10 @@ protected function setRules($handle, $data, $index) ->validator() ->withContext([ 'prefix' => $this->field->validationContext('prefix').$this->setRuleFieldPrefix($index).'.', - ]) + ]); + + $rules = $this + ->addEntryValidationReplacements($this->field, $rules) ->rules(); return collect($rules)->mapWithKeys(function ($rules, $handle) use ($index) {