Checkbox List Default

Checkboxlist default() does not work in EditRecord. How to resolve it?
8 Replies
โ„ฌโ„ด๐’ฟ๐’ฟ๐’พ
CheckboxList::make('Juice')
->options([
0 => 'Apple juice',
1 => 'Mango juice'
])->default([0,1])
CheckboxList::make('Juice')
->options([
0 => 'Apple juice',
1 => 'Mango juice'
])->default([0,1])
cheesegrits
cheesegritsโ€ข10mo ago
Defaults don't get applied when editing records. They are only applied when creating.
โ„ฌโ„ด๐’ฟ๐’ฟ๐’พ
so how to apply the default when editing record?
Patrick Boivin
Patrick Boivinโ€ข10mo ago
Something like this
->afterStateHydrated(function ($state, $component) {
if (is_null($state)) {
$component->state(...);
}
})
->afterStateHydrated(function ($state, $component) {
if (is_null($state)) {
$component->state(...);
}
})
โ„ฌโ„ด๐’ฟ๐’ฟ๐’พ
thank you, sir. I try
cheesegrits
cheesegritsโ€ข10mo ago
Just bear in mind that this will override any value that field previously had. Which is why default() only applies when creating, when by definition fields don't yet have values.
Patrick Boivin
Patrick Boivinโ€ข10mo ago
That's why the check for is_null() (or something similar depending on the field) is needed