From 9771b23d1d242721e5805aba2a788008bffa0020 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 16:52:55 +0100 Subject: [PATCH 01/10] Remove old validation rules from Button Group & Select fieldtypes These rules were ensuring all options had keys. Since these rules were added, #9834 was merged, which added key validation to the base array fieldtype. --- src/Fieldtypes/ButtonGroup.php | 7 ------- src/Fieldtypes/Select.php | 7 ------- 2 files changed, 14 deletions(-) diff --git a/src/Fieldtypes/ButtonGroup.php b/src/Fieldtypes/ButtonGroup.php index 70cc827638..f071746b20 100644 --- a/src/Fieldtypes/ButtonGroup.php +++ b/src/Fieldtypes/ButtonGroup.php @@ -23,13 +23,6 @@ protected function configFieldItems(): array 'type' => 'array', 'value_header' => __('Label').' ('.__('Optional').')', 'add_button' => __('Add Option'), - 'validate' => [function ($attribute, $value, $fail) { - $optionsWithoutKeys = collect($value)->keys()->filter(fn ($key) => empty($key) || $key === 'null'); - - if ($optionsWithoutKeys->isNotEmpty()) { - $fail(__('statamic::validation.options_require_keys')); - } - }], ], 'default' => [ 'display' => __('Default Value'), diff --git a/src/Fieldtypes/Select.php b/src/Fieldtypes/Select.php index 5288044c72..8e0d45f3b6 100644 --- a/src/Fieldtypes/Select.php +++ b/src/Fieldtypes/Select.php @@ -25,13 +25,6 @@ protected function configFieldItems(): array 'key_header' => __('Key'), 'value_header' => __('Label').' ('.__('Optional').')', 'add_button' => __('Add Option'), - 'validate' => [function ($attribute, $value, $fail) { - $optionsWithoutKeys = collect($value)->keys()->filter(fn ($key) => empty($key) || $key === 'null'); - - if ($optionsWithoutKeys->isNotEmpty()) { - $fail(__('statamic::validation.options_require_keys')); - } - }], ], 'taggable' => [ 'display' => __('Allow additions'), From b69669e2b0f831643fcef0402524116bdef6e1d3 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 16:53:58 +0100 Subject: [PATCH 02/10] The Array Fieldtype checks against `null`, not empty strings --- tests/Fieldtypes/ButtonGroupTest.php | 2 +- tests/Fieldtypes/SelectTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index 5aca87df76..cce6600115 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -31,7 +31,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'options' => [ 'one' => 'One', 'two' => 'Two', - '' => 'Three', + 'null' => 'Three', ], ]); diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 84127456e3..4d7815ff8d 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -31,7 +31,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'options' => [ 'one' => 'One', 'two' => 'Two', - '' => 'Three', + 'null' => 'Three', ], ]); From c07884be879b0b6aeeb59c0011ab2c8ee50af5fb Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 16:54:47 +0100 Subject: [PATCH 03/10] Drop "all options must have values" validation --- src/Fieldtypes/Arr.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Fieldtypes/Arr.php b/src/Fieldtypes/Arr.php index 97474c3d0f..d56d1bed24 100644 --- a/src/Fieldtypes/Arr.php +++ b/src/Fieldtypes/Arr.php @@ -95,10 +95,6 @@ public function rules(): array if ($values->has('null')) { $fail('statamic::validation.arr_fieldtype')->translate(); } - - if ($values->count() !== $values->reject(fn ($v) => is_null($v))->count()) { - $fail('statamic::validation.arr_fieldtype')->translate(); - } }]; } } From 5147f30343172ee4b8ced3654c4c54fec5e2c168 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 16:55:21 +0100 Subject: [PATCH 04/10] Add tests to ensure options can be configured without labels For Button Group & Select fieldtypes --- tests/Fieldtypes/ButtonGroupTest.php | 22 ++++++++++++++++++++++ tests/Fieldtypes/SelectTest.php | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index cce6600115..de76f4b01a 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -59,4 +59,26 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } + + /** @test */ + public function does_not_throw_a_validation_error_when_label_is_missing_from_option() + { + $fieldtype = FieldtypeRepository::find('button_group'); + $blueprint = $fieldtype->configBlueprint(); + + $fields = $blueprint + ->fields() + ->addValues([ + 'options' => [ + 'one' => null, + 'two' => null, + ], + ]); + + $fields->validate(); + + // If we've made it this far, it means we've passed validation + // (otherwise an exception would be thrown). + $this->assertTrue(true); + } } diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 4d7815ff8d..88b40b04e9 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -59,4 +59,27 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } + + /** @test */ + /** @test */ + public function does_not_throw_a_validation_error_when_label_is_missing_from_option() + { + $fieldtype = FieldtypeRepository::find('select'); + $blueprint = $fieldtype->configBlueprint(); + + $fields = $blueprint + ->fields() + ->addValues([ + 'options' => [ + 'one' => null, + 'two' => null, + ], + ]); + + $fields->validate(); + + // If we've made it this far, it means we've passed validation + // (otherwise an exception would be thrown). + $this->assertTrue(true); + } } From 99fe1e95ca53f068fcc4dca021b65cc7e8932e88 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 17:07:16 +0100 Subject: [PATCH 05/10] Improve the validation message --- src/Fieldtypes/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fieldtypes/Arr.php b/src/Fieldtypes/Arr.php index d56d1bed24..74387e22bb 100644 --- a/src/Fieldtypes/Arr.php +++ b/src/Fieldtypes/Arr.php @@ -93,7 +93,7 @@ public function rules(): array $values = collect($value); if ($values->has('null')) { - $fail('statamic::validation.arr_fieldtype')->translate(); + $fail('statamic::validation.options_require_keys')->translate(); } }]; } From c3aded6e5a490ad05f4ec3394b325155e7751d5e Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 17:07:39 +0100 Subject: [PATCH 06/10] Remove duplicate --- tests/Fieldtypes/SelectTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 88b40b04e9..bb983a3b61 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -59,8 +59,7 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } - - /** @test */ + /** @test */ public function does_not_throw_a_validation_error_when_label_is_missing_from_option() { From 8a1f9e1edf150482c5f0287c0a09df5cedad43fe Mon Sep 17 00:00:00 2001 From: duncanmcclean Date: Wed, 19 Jun 2024 16:09:18 +0000 Subject: [PATCH 07/10] Fix styling --- tests/Fieldtypes/SelectTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index bb983a3b61..2f4feaf9bc 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -59,7 +59,7 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } - + /** @test */ public function does_not_throw_a_validation_error_when_label_is_missing_from_option() { From 57e6955101ab438865d6f609db0fce4bcff82cc1 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 17:35:53 +0100 Subject: [PATCH 08/10] Add empty string keys to tests --- tests/Fieldtypes/ButtonGroupTest.php | 1 + tests/Fieldtypes/SelectTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index de76f4b01a..1f2418ae3c 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -32,6 +32,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'one' => 'One', 'two' => 'Two', 'null' => 'Three', + '' => null, ], ]); diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 2f4feaf9bc..0330f572c8 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -32,6 +32,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'one' => 'One', 'two' => 'Two', 'null' => 'Three', + '' => null, ], ]); From 4c8951d7f3e6b36d1c82ed5e58b55dddf2839efa Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 19 Jun 2024 17:41:09 +0100 Subject: [PATCH 09/10] These should have values, like the `null` examples --- tests/Fieldtypes/ButtonGroupTest.php | 2 +- tests/Fieldtypes/SelectTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index 1f2418ae3c..4ecb0ae371 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -32,7 +32,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'one' => 'One', 'two' => 'Two', 'null' => 'Three', - '' => null, + '' => 'Four', ], ]); diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 0330f572c8..0d3e4837a2 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -32,7 +32,7 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'one' => 'One', 'two' => 'Two', 'null' => 'Three', - '' => null, + '' => 'Four', ], ]); From db3805d2237e39d53ad85143a6f255aece20edf0 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 16 Jul 2024 12:11:20 +0100 Subject: [PATCH 10/10] use `#[Test]` instead of metadata --- tests/Fieldtypes/ButtonGroupTest.php | 2 +- tests/Fieldtypes/SelectTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index 38b3d46d3e..329ea207be 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -62,7 +62,7 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } - /** @test */ + #[Test] public function does_not_throw_a_validation_error_when_label_is_missing_from_option() { $fieldtype = FieldtypeRepository::find('button_group'); diff --git a/tests/Fieldtypes/SelectTest.php b/tests/Fieldtypes/SelectTest.php index 5939747b91..f019d6d015 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -62,7 +62,7 @@ public function does_not_throw_a_validation_error_when_all_options_have_keys() $this->assertEquals($values, $fields->validate()); } - /** @test */ + #[Test] public function does_not_throw_a_validation_error_when_label_is_missing_from_option() { $fieldtype = FieldtypeRepository::find('select');