Disabled field fails validation in CheckboxList
Package
filament/filament
Package Version
v4.x.x
Laravel Version
v12.38.1
Livewire Version
v3.6.4
PHP Version
PHP 8.4.6
Problem description
When using disableOptionWhen() on a CheckboxList, validation fails for disabled options even though they are checked and submitted with the form.
The issue occurs because the validation only checks against enabled options, excluding any disabled ones. This causes the validation to reject values from disabled checkboxes that are legitimately selected and submitted.
Solution in my proyect
add ->in(fn (CheckboxList $component): array => array_keys($component->getOptions()))
Expected behavior
The documentation or examples should clarify that when validating a CheckboxList with disabled options, getOptions() should be used instead of getEnabledOptions() in the ->in() validation rule.
Steps to reproduce
- Create a CheckboxList form field with multiple options
- Use disableOptionWhen() to disable at least one option that will be checked
- Submit the form with the disabled option checked
- Observe that validation fails with the message: The form_items field is not in the list of allowed values.
use Filament\Forms\Components\CheckboxList;
CheckboxList::make('form_items') ->options([ 'name' => 'Name', 'email' => 'Email', 'phone' => 'Phone', ]) ->disableOptionWhen(fn (string $value): bool => $value === 'email');
Reproduction repository (issue will be closed if this is not valid)
https://github.com/liberty-technologies/managed-wifi
Relevant log output
Please make the reproduction repo public, and I will reopen this if you @ me
@danharrin Hi, I created a small Laravel project so you can see what I’m referring to. This is the public link:
https://github.com/JoelChoy0/Checklist-filament
May I?
Yes, no need to ask when it is marked as Bug, especially help wanted :)
@danharrin this is a bug but I'm not sure it's the same @JoelChoy0 mentioned: if a checkbox is disabled (checked), I do not think it must be submitted. The package works as supposed to.
What happens here, it's the validation fails even if I check an enable checkbox, and I will try to fix that.
What you think?
I am sorry @giacomomasseron I do not understand what you meant there
This form fails validation:
CheckboxList::make('form_items')
->options([
'name' => 'Name',
'email' => 'Email',
'phone' => 'Phone',
])
->default(['email'])
->columns()
->disableOptionWhen(fn (string $value): bool => $value === 'email')
I think it shouldn't fail, because there are no validation rules.
If I use this:
CheckboxList::make('form_items')
->options([
'name' => 'Name',
'email' => 'Email',
'phone' => 'Phone',
])
->default(['email'])
->columns()
->in(fn (CheckboxList $component): array => array_keys($component->getEnabledOptions())) // Added validation rule
->disableOptionWhen(fn (string $value): bool => $value === 'email')
the form fails even if I check an enabled checkbox (and I think this is the bug mentioned).