F
Filament3w ago
Koda

Disable/Hide Checkbox item in CheckboxList

I use CheckboxList / RadioDeck. Let's say I have 5 Values A, B, C, D and E. Gives a solution to uncheck,disable(or hide) C and D if I check A, and reverse if I uncehck A?
9 Replies
Koda
Koda3w ago
@Leandro Ferreira Thank you. Is disableOptionWhen not only to disable an option on page load? I need multiple Rule To disable D if I check A, Disable D and E if I check B,...
LeandroFerreira
did you make it live()?
Koda
Koda3w ago
Thank you. I forgot live(). But what is the solution to uncheck and disable or hide? I thought I found a solution with false but this is wrong
CheckboxList::make('options')
->options([
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
])
->live()
->disableOptionWhen(function($value, Set $set, $state) {
if(in_array('a', $state)) {
//if($value === 'a') { # value is allways a...?
$set('options.b', false);
}
}),
CheckboxList::make('options')
->options([
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
])
->live()
->disableOptionWhen(function($value, Set $set, $state) {
if(in_array('a', $state)) {
//if($value === 'a') { # value is allways a...?
$set('options.b', false);
}
}),
LeandroFerreira
->disableOptionWhen(fn (string $value, ?array $state): bool => in_array('a', $state) && $value === 'b')
->disableOptionWhen(fn (string $value, ?array $state): bool => in_array('a', $state) && $value === 'b')
Koda
Koda3w ago
Thank you. This work. Have you also a solution to uncheck the value for a better user experience?
toeknee
toeknee3w ago
I would get the current values, and then remove the ones you are disabiling and set it
Koda
Koda3w ago
@toeknee Thank you. have you an example?
toeknee
toeknee3w ago
DisableOpenWhen should actually do that. But if it's not just do:
CheckboxList::make('options')
->options([
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
])
->live()
->disableOptionWhen(function($value, Set $set, $state) {
if(in_array('a', $state) && $state['a']) {
$set('options.b', false);
}
}),
CheckboxList::make('options')
->options([
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
])
->live()
->disableOptionWhen(function($value, Set $set, $state) {
if(in_array('a', $state) && $state['a']) {
$set('options.b', false);
}
}),