diff --git a/src/Fieldtypes/Arr.php b/src/Fieldtypes/Arr.php index 97474c3d0f..74387e22bb 100644 --- a/src/Fieldtypes/Arr.php +++ b/src/Fieldtypes/Arr.php @@ -93,11 +93,7 @@ public function rules(): array $values = collect($value); 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(); + $fail('statamic::validation.options_require_keys')->translate(); } }]; } 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'), diff --git a/tests/Fieldtypes/ButtonGroupTest.php b/tests/Fieldtypes/ButtonGroupTest.php index 4786180536..329ea207be 100644 --- a/tests/Fieldtypes/ButtonGroupTest.php +++ b/tests/Fieldtypes/ButtonGroupTest.php @@ -32,7 +32,8 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'options' => [ 'one' => 'One', 'two' => 'Two', - '' => 'Three', + 'null' => 'Three', + '' => 'Four', ], ]); @@ -60,4 +61,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 06eb099bae..f019d6d015 100644 --- a/tests/Fieldtypes/SelectTest.php +++ b/tests/Fieldtypes/SelectTest.php @@ -32,7 +32,8 @@ public function throws_a_validation_error_when_key_is_missing_from_option() 'options' => [ 'one' => 'One', 'two' => 'Two', - '' => 'Three', + 'null' => 'Three', + '' => 'Four', ], ]); @@ -60,4 +61,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('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); + } }