From 5b8a4e2634c00c32e90fc3d77aac5567d2875eb4 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Tue, 14 May 2024 09:56:44 +0200 Subject: [PATCH] feat: add validation replacements to replicator and grid field types --- src/Fieldtypes/AddValidationReplacements.php | 25 ++++++++++++++++++++ src/Fieldtypes/Grid.php | 7 +++++- src/Fieldtypes/Replicator.php | 7 +++++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/Fieldtypes/AddValidationReplacements.php diff --git a/src/Fieldtypes/AddValidationReplacements.php b/src/Fieldtypes/AddValidationReplacements.php new file mode 100644 index 0000000000..5a34eb99cb --- /dev/null +++ b/src/Fieldtypes/AddValidationReplacements.php @@ -0,0 +1,25 @@ +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 5f4783d586..ea3f1d5328 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -14,6 +14,8 @@ class Grid extends Fieldtype { + use AddValidationReplacements; + protected $categories = ['structured']; protected $defaultable = false; protected $defaultValue = []; @@ -159,7 +161,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 a437022e07..96f6b4a857 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -15,6 +15,8 @@ class Replicator extends Fieldtype { + use AddValidationReplacements; + protected $categories = ['structured']; protected $defaultValue = []; protected $rules = ['array']; @@ -138,7 +140,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) {