Skip to content

Commit

Permalink
[5.x] Allow options without labels in the select etc fieldtypes (#10336)
Browse files Browse the repository at this point in the history
Co-authored-by: duncanmcclean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent b12d683 commit 416e970
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/Fieldtypes/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}];
}
Expand Down
7 changes: 0 additions & 7 deletions src/Fieldtypes/ButtonGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
7 changes: 0 additions & 7 deletions src/Fieldtypes/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
25 changes: 24 additions & 1 deletion tests/Fieldtypes/ButtonGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);

Expand Down Expand Up @@ -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);
}
}
25 changes: 24 additions & 1 deletion tests/Fieldtypes/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 416e970

Please sign in to comment.