F
Filamentβ€’2y ago
Kuldeep

Make a toggle required based on the other 3 fields

Hey I wanted to change the toggle required/not required based on the change of select value. From the select value, I'm having 3 values on which the selection toggle is required and the rest of the values toggle is not required. This will be the initial case. I've to update the toggle based on 3 other fields. My code is attached. How can I do it?
11 Replies
Kuldeep
KuldeepOPβ€’2y ago
Forms\Components\Select::make('field1')
->label('Field 1')
->options([
'OP1' => 'OP1',
'OP2' => 'OP2',
'OP3' => 'OP3',
])
->reactive()
->required(),
Forms\Components\Select::make('field2')
->label('Field 2')
->options([
'OP1' => 'OP1',
'OP2' => 'OP2',
'OP3' => 'OP3',
])
->reactive()
->required(),
Forms\Components\Select::make('check1')
->label('Check 1')
->options(['YES' => 'Yes', 'NO' => 'No'])
->default('NO')
->reactive()
->required(),
Forms\Components\Select::make('check2')
->label('Check 2')
->options(['YES' => 'Yes', 'NO' => 'No'])
->default('NO')
->reactive()
->required(),
Forms\Components\Toggle::make('required_field')
->reactive()
->required(fn (callable $get): bool => in_array($get('field1'), ['OP2', OP3]) || in_array($get('field2'), ['OP2', OP3])),
Forms\Components\Select::make('field1')
->label('Field 1')
->options([
'OP1' => 'OP1',
'OP2' => 'OP2',
'OP3' => 'OP3',
])
->reactive()
->required(),
Forms\Components\Select::make('field2')
->label('Field 2')
->options([
'OP1' => 'OP1',
'OP2' => 'OP2',
'OP3' => 'OP3',
])
->reactive()
->required(),
Forms\Components\Select::make('check1')
->label('Check 1')
->options(['YES' => 'Yes', 'NO' => 'No'])
->default('NO')
->reactive()
->required(),
Forms\Components\Select::make('check2')
->label('Check 2')
->options(['YES' => 'Yes', 'NO' => 'No'])
->default('NO')
->reactive()
->required(),
Forms\Components\Toggle::make('required_field')
->reactive()
->required(fn (callable $get): bool => in_array($get('field1'), ['OP2', OP3]) || in_array($get('field2'), ['OP2', OP3])),
Tried this. required() works but form is submitting without toggle check
LeandroFerreira
LeandroFerreiraβ€’2y ago
could be better, but didn't it work?
->required(function ($get) {
return $get('field1') === 'OP1' || $get('field1') === 'OP2' || $get('field2') === 'OP2';
})
->required(function ($get) {
return $get('field1') === 'OP1' || $get('field1') === 'OP2' || $get('field2') === 'OP2';
})
Kuldeep
KuldeepOPβ€’2y ago
Yes, this also works. The field is required() properly. But when I submit the form without checking that toggle, it's not displaying me an error message. Validation failed for the required() is not working.
LeandroFerreira
LeandroFerreiraβ€’2y ago
toogle is false/true... false/true is a value. Do you need to check if it is true/checked?
Kuldeep
KuldeepOPβ€’2y ago
true
LeandroFerreira
LeandroFerreiraβ€’2y ago
?
Kuldeep
KuldeepOPβ€’2y ago
Yes, I wanted to check it is true or not.
LeandroFerreira
LeandroFerreiraβ€’2y ago
change required to accepted
Kuldeep
KuldeepOPβ€’2y ago
If false then give a validation message And if true pass the form and submit
LeandroFerreira
LeandroFerreiraβ€’2y ago
try accepted instead of required
Kuldeep
KuldeepOPβ€’2y ago
Thank you πŸ™‚
Want results from more Discord servers?
Add your server