F
Filament14mo ago
dwiser

$set all checked in nested CheckboxList

I am creating a form section visually similar to the bezhansalleh/filament-shield package but for project notification options instead of spatie permissions. When creating a user, there is a Section for every project, each including a Toggle and a CheckboxList to choose individual notifications or all in bulk. Changing the Toggle will update all the Checkboxes to the same state and unchecking any one Checkbox will change the Toggle to off. I can't seem to find a way to reference each individual CheckboxList item using $set. A bulk toggle of the checkbox list would likely solve my issue but I'm using the opportunity to better understand filament and keep the preferred UI instead of fall back to the bulk toggle method.
Solution:
I've already re-written most of the form but in the process I think I figured out the problem. I assumed the CheckboxList would create an associative array: ```php [ 'Bid Request Received', 'Bid Sent',...
Jump to solution
3 Replies
dwiser
dwiserOP14mo ago
Code:
Forms\Components\Section::make()
->schema([
Forms\Components\Toggle::make('projects.' . $project->name)
->onIcon('heroicon-s-shield-check')
->offIcon('heroicon-m-x-mark')
->label($project->label)
->helperText('Enable all Notifications for this project.')
->live(onBlur: true)
->afterStateUpdated(function ($state, Forms\Set $set) use ($project) {
foreach($project->notifications as $notification)
{
// For every notification associated with this Project; Set the checkbox state to match the Toggle state
$set('notifications .' . $notification->name, $state);
}
}),
Forms\Components\Fieldset::make($project->name . 'notifications ')
->label('Notifications')
->schema([
Forms\Components\CheckboxList::make('notifications ')
->label(false)
->options($project->notifications ->pluck('label', 'name'))
->live(onBlur: true)
->afterStateUpdated(function ($state) {
// If any checkbox is unchecked, mark the Toggle as off.
}),
])

])
->columnSpan(1)
Forms\Components\Section::make()
->schema([
Forms\Components\Toggle::make('projects.' . $project->name)
->onIcon('heroicon-s-shield-check')
->offIcon('heroicon-m-x-mark')
->label($project->label)
->helperText('Enable all Notifications for this project.')
->live(onBlur: true)
->afterStateUpdated(function ($state, Forms\Set $set) use ($project) {
foreach($project->notifications as $notification)
{
// For every notification associated with this Project; Set the checkbox state to match the Toggle state
$set('notifications .' . $notification->name, $state);
}
}),
Forms\Components\Fieldset::make($project->name . 'notifications ')
->label('Notifications')
->schema([
Forms\Components\CheckboxList::make('notifications ')
->label(false)
->options($project->notifications ->pluck('label', 'name'))
->live(onBlur: true)
->afterStateUpdated(function ($state) {
// If any checkbox is unchecked, mark the Toggle as off.
}),
])

])
->columnSpan(1)
Solution
dwiser
dwiser14mo ago
I've already re-written most of the form but in the process I think I figured out the problem. I assumed the CheckboxList would create an associative array:
[
'Bid Request Received',
'Bid Sent',
// ...
]
[
'Bid Request Received',
'Bid Sent',
// ...
]
dwiser
dwiserOP14mo ago
What was actually getting submitted with the form was a numeric array:
[
0 => 'Bid Request Received',
1 => 'Bid Sent',
2 = > // ...
]
[
0 => 'Bid Request Received',
1 => 'Bid Sent',
2 = > // ...
]
This meant that
$get('notifications .' . $notification->name)
$get('notifications .' . $notification->name)
was not the correct way to set the values and instead it needed to be pushed to the
$get('notifications')
$get('notifications')
array as a numeric array.
Want results from more Discord servers?
Add your server