From 5b8a4e2634c00c32e90fc3d77aac5567d2875eb4 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Tue, 14 May 2024 09:56:44 +0200 Subject: [PATCH 1/3] 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) { From a737397620de446474bd0d6512f0281d775eb5b3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 5 Dec 2024 15:07:40 -0500 Subject: [PATCH 2/3] Rename --- ...tionReplacements.php => AddsEntryValidationReplacements.php} | 2 +- src/Fieldtypes/Grid.php | 2 +- src/Fieldtypes/Replicator.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/Fieldtypes/{AddValidationReplacements.php => AddsEntryValidationReplacements.php} (93%) diff --git a/src/Fieldtypes/AddValidationReplacements.php b/src/Fieldtypes/AddsEntryValidationReplacements.php similarity index 93% rename from src/Fieldtypes/AddValidationReplacements.php rename to src/Fieldtypes/AddsEntryValidationReplacements.php index 5a34eb99cb..810695f9fd 100644 --- a/src/Fieldtypes/AddValidationReplacements.php +++ b/src/Fieldtypes/AddsEntryValidationReplacements.php @@ -6,7 +6,7 @@ use Statamic\Fields\Field; use Statamic\Fields\Validator; -trait AddValidationReplacements +trait AddsEntryValidationReplacements { protected function addEntryValidationReplacements(Field $field, Validator $rules): Validator { diff --git a/src/Fieldtypes/Grid.php b/src/Fieldtypes/Grid.php index 37376a0f94..c9783322d9 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -14,7 +14,7 @@ class Grid extends Fieldtype { - use AddValidationReplacements; + use AddsEntryValidationReplacements; protected $categories = ['structured']; protected $defaultable = false; diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index a3c2dcd7bd..ee6e1a5722 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -16,7 +16,7 @@ class Replicator extends Fieldtype { - use AddValidationReplacements; + use AddsEntryValidationReplacements; protected $categories = ['structured']; protected $keywords = ['builder', 'page builder', 'content']; From 8560641676c0c47953d36a59d744ec60b8b975f5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 5 Dec 2024 15:10:30 -0500 Subject: [PATCH 3/3] Add todo with explanation --- src/Fieldtypes/AddsEntryValidationReplacements.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Fieldtypes/AddsEntryValidationReplacements.php b/src/Fieldtypes/AddsEntryValidationReplacements.php index 810695f9fd..3fbd97d938 100644 --- a/src/Fieldtypes/AddsEntryValidationReplacements.php +++ b/src/Fieldtypes/AddsEntryValidationReplacements.php @@ -6,6 +6,13 @@ use Statamic\Fields\Field; use Statamic\Fields\Validator; +/** + * TODO + * This allows Grid/Replicator/Bard fields to add validation replacements. + * It adds the same replacements that get added in EntriesController@update. + * Ideally those would get passed down into the field automatically somehow, + * so this can be considered a workaround until that happens. + */ trait AddsEntryValidationReplacements { protected function addEntryValidationReplacements(Field $field, Validator $rules): Validator