Select Option Disabled Based on Another Field's Value

Code:
Select::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->live(),

Select::make('name')
->options([
'jack' => 'Jack',
'bob' => 'Bob'
])
->disableOptionWhen(fn($get, $value) => $get('status') === 'published' && $value === 'bob')
->live()
->required(),
Select::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->live(),

Select::make('name')
->options([
'jack' => 'Jack',
'bob' => 'Bob'
])
->disableOptionWhen(fn($get, $value) => $get('status') === 'published' && $value === 'bob')
->live()
->required(),
Expected Behaviour: - When the status is set to published, the Name field should disable the option bob. - Selecting bob when the status is not published should be allowed, but if the status is changed to published, bob should be disabled and should not remain selected. Observed Behaviour 1. Set the status to any value except published. 2. Select name as bob. 3. Change the status to published. 4. Notice that bob is still selected and the record can still be saved with bob as the selected value, even though the option should be disabled. Complication: - The issue I believe here is, when the status is changed to published, the form still allows bob to be saved, even though the option is disabled. Question: - Should this behaviour be automatically handled by Filament (Disabling/Resetting the option dynamically based on the value of another field)? - Or, do I need to manually validate the data when saving to ensure that bob is not selected when status is set to published?
Solution:
You’re not resetting the name field. So the name in the data is still bob. So you need to $set(‘name’, null) in the status afterStateUpdated() too.
Jump to solution
3 Replies
gemini.dev
gemini.devOP2w ago
No description
Solution
awcodes
awcodes2w ago
You’re not resetting the name field. So the name in the data is still bob. So you need to $set(‘name’, null) in the status afterStateUpdated() too.
gemini.dev
gemini.devOP6d ago
Thank you @awcodes 🫡

Did you find this page helpful?