Checkbox List Default

Checkboxlist default() does not work in EditRecord. How to resolve it?
11 Replies
ℬℴ𝒿𝒿𝒾
ℬℴ𝒿𝒿𝒾OP15mo ago
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])
ℬℴ𝒿𝒿𝒾
ℬℴ𝒿𝒿𝒾OP15mo ago
No description
cheesegrits
cheesegrits15mo ago
Defaults don't get applied when editing records. They are only applied when creating.
ℬℴ𝒿𝒿𝒾
ℬℴ𝒿𝒿𝒾OP15mo ago
so how to apply the default when editing record?
Patrick Boivin
Patrick Boivin15mo ago
Something like this
->afterStateHydrated(function ($state, $component) {
if (is_null($state)) {
$component->state(...);
}
})
->afterStateHydrated(function ($state, $component) {
if (is_null($state)) {
$component->state(...);
}
})
ℬℴ𝒿𝒿𝒾
ℬℴ𝒿𝒿𝒾OP15mo ago
thank you, sir. I try
cheesegrits
cheesegrits15mo 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 Boivin15mo ago
That's why the check for is_null() (or something similar depending on the field) is needed
pocket.racer
pocket.racer2mo 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
LeandroFerreira2mo ago
are you using the panel builder or only the form builder?
pocket.racer
pocket.racer2mo ago
panel builder create resource page looks like i solved it by not using ->default() on the create page, time to sleep thank you
Want results from more Discord servers?
Add your server