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) {