Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Allow options without labels in the Button Group & Select fieldtypes #10336

Merged
merged 12 commits into from
Aug 9, 2024
Merged
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);
}
}
Loading