Checkbox List Default

Checkboxlist default() does not work in EditRecord. How to resolve it?
11 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
cheesegrits2y 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
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
cheesegrits2y 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
That's why the check for is_null() (or something similar depending on the field) is needed
pocket.racer
pocket.racer5mo ago
->default() did not get applied when I'm creating too for Checkboxlist My code is something like this
Select::make('checklist_template_id')
->label('Checklist Template')
->live()
->visible(fn (Get $get) => $get('model_id') !== null)
->relationship('checklistTemplate', 'name'),

CheckboxList::make('results')
->live()
->options(function (Get $get): array {

$checklistTemplateId = $get('checklist_template_id');

$checklistTemplate = ChecklistTemplate::find($checklistTemplateId);

if (! $checklistTemplate) {
return [];
}

if ($checklistTemplate->checklist_class) {
$checklistClass = ChecklistManager::getChecklistClass($checklistTemplate->checklist_class);

$checklistObject = new $checklistClass(Order::find($get('model_id')));

return $checklistObject->getKeys();
}

return $checklistTemplate->checklist_keys;
})
->default(function (Get $get) {
ray('checklist-default-called');
$checklistTemplateId = $get('checklist_template_id');

$checklistTemplate = ChecklistTemplate::find($checklistTemplateId);

if (! $checklistTemplate) {
return [];
}

if ($checklistTemplate->checklist_class) {
$checklistClass = ChecklistManager::getChecklistClass($checklistTemplate->checklist_class);

$checklistObject = new $checklistClass(Order::find($get('model_id')));

$result = $checklistObject->getKeysThatAreTrueFromAutoChecks();

return $result;

}

return [];
})
->visible(fn (Get $get): bool => $get('checklist_template_id') !== null)
->bulkToggleable(),
Select::make('checklist_template_id')
->label('Checklist Template')
->live()
->visible(fn (Get $get) => $get('model_id') !== null)
->relationship('checklistTemplate', 'name'),

CheckboxList::make('results')
->live()
->options(function (Get $get): array {

$checklistTemplateId = $get('checklist_template_id');

$checklistTemplate = ChecklistTemplate::find($checklistTemplateId);

if (! $checklistTemplate) {
return [];
}

if ($checklistTemplate->checklist_class) {
$checklistClass = ChecklistManager::getChecklistClass($checklistTemplate->checklist_class);

$checklistObject = new $checklistClass(Order::find($get('model_id')));

return $checklistObject->getKeys();
}

return $checklistTemplate->checklist_keys;
})
->default(function (Get $get) {
ray('checklist-default-called');
$checklistTemplateId = $get('checklist_template_id');

$checklistTemplate = ChecklistTemplate::find($checklistTemplateId);

if (! $checklistTemplate) {
return [];
}

if ($checklistTemplate->checklist_class) {
$checklistClass = ChecklistManager::getChecklistClass($checklistTemplate->checklist_class);

$checklistObject = new $checklistClass(Order::find($get('model_id')));

$result = $checklistObject->getKeysThatAreTrueFromAutoChecks();

return $result;

}

return [];
})
->visible(fn (Get $get): bool => $get('checklist_template_id') !== null)
->bulkToggleable(),
LeandroFerreira
LeandroFerreira5mo ago
are you using the panel builder or only the form builder?
pocket.racer
pocket.racer5mo ago
panel builder create resource page looks like i solved it by not using ->default() on the create page, time to sleep thank you

Did you find this page helpful?