F
Filament16mo ago
Csinesz

CheckboxList options set some value to checked

How can I set some value to checked?
41 Replies
Yuut4
Yuut416mo ago
you just need to return true or false in the values ​​you want you scroll through the array and the values ​​you need return true
Csinesz
CsineszOP16mo ago
key value? But how can I set Label, value and the checkbox status?
Yuut4
Yuut416mo ago
try to use $state in you function
Csinesz
CsineszOP16mo ago
Could you give me an example? I can understand the state and the return value return [ 'not_1' => 'All notification', 'not_2' => 'Some notification', 'not_e' => 'No notification', ]; return need to looke like this, otherwise it gives me an error
Yuut4
Yuut416mo ago
for example the state will fetch the value of the checkbox if I'm not in error let me check
Csinesz
CsineszOP16mo ago
if the $notification->not_1 = true I need to set not_1 return value to true
Csinesz
CsineszOP16mo ago
some how 🙂
Yuut4
Yuut416mo ago
oh, wait is acheckboxlist, so it might be different than what I'm thinking
Csinesz
CsineszOP16mo ago
yes it's a list I get the userId and some other Id - whit these id's I query the notifications - check the values and set the checkboxes I read all the documentations bot not fund anything
Yuut4
Yuut416mo ago
I'll look for something to see if it helps
Csinesz
CsineszOP16mo ago
🙏
Bruno Pereira
Bruno Pereira16mo ago
What values you want the checked to have and what is the condition to handle those values?
Csinesz
CsineszOP16mo ago
if ($notification->not_1) is true than I would like to return options [not_1] value set false ot true CheckboxList::make('notifications') ->options(function (callable $get) { $userId = $get('userId'); $powerPlantId = $get('power_plant_id'); $notifications = UserPowerPlant::where('user_id', $userId)->where('power_plant_id', $powerPlantId)->get(); return [ 'not_1' => 'All notification', 'not_2' => 'Some notification', 'not_e' => 'No notification', ]; })->dehydrated(false), my return contains 3 value and those values needed to checked or not
Bruno Pereira
Bruno Pereira16mo ago
If in your model you have the column casted as boolean all you need to do is 'checked' => $row->column_name
Csinesz
CsineszOP16mo ago
but where should I set the checked value? options need to return with a simple array (key=> value) pair
Bruno Pereira
Bruno Pereira16mo ago
Do you have a column for each option on your db table? Can you show a var dump of your $row somehow?
Csinesz
CsineszOP16mo ago
Yes of course just a sec [2023-08-07 20:21:32] local.INFO: [{ "active_schedule_notification":false, "active_billing_notification":true, "active_datasheet_notification":false, "active_billing_sample_notification":false, ... }] this a a row from nitifications and every row has a label too
Bruno Pereira
Bruno Pereira16mo ago
And where do you get the info for not_1, not_2, not_3 ?
Csinesz
CsineszOP16mo ago
not_1, not_2 was just an example "active_schedule_notification":false, "active_billing_notification":true, "active_datasheet_notification":false, "active_billing_sample_notification":false, is the real value
Bruno Pereira
Bruno Pereira16mo ago
Oh I see. Gotcha... Let me see if I can come up with something
Csinesz
CsineszOP16mo ago
🙏
LeandroFerreira
LeandroFerreira16mo ago
would you like to set default on the CreatePage?
CheckboxList::make('notifications')
->options([
'not_1' => 'All notification',
'not_2' => 'Some notification',
'not_e' => 'No notification',
])
->default(['not_1', 'not_2'])
CheckboxList::make('notifications')
->options([
'not_1' => 'All notification',
'not_2' => 'Some notification',
'not_e' => 'No notification',
])
->default(['not_1', 'not_2'])
?
Bruno Pereira
Bruno Pereira16mo ago
Yup, this is what I was getting to
LeandroFerreira
LeandroFerreira16mo ago
if it worked, mark the topic as solved please
Bruno Pereira
Bruno Pereira16mo ago
It's not my post ahaha
LeandroFerreira
LeandroFerreira16mo ago
ahh sorry 😅
Csinesz
CsineszOP16mo ago
It's not working - and only in options (callable) I know which should checked
Csinesz
CsineszOP16mo ago
Bruno Pereira
Bruno Pereira16mo ago
I don't think that you can achieve that with a checkboxlist tbh... The options method doesn't handle the checked status, only value and label
Csinesz
CsineszOP16mo ago
hm - is it impossible? hmmm why? I think is not a big thing ...but yeah - I can't find anything 😦 default(true) isnt work too
Bruno Pereira
Bruno Pereira16mo ago
Impossible? Don't know I'm not a filament dev xD it may not be a big thing but you have to take in consideration the limitations of the package you're using
Andrew Wallo
Andrew Wallo16mo ago
Default values don’t work with checkbox list. Try dehydrating the field with your default values you want applied.
Csinesz
CsineszOP16mo ago
could you give me an example? here is my code:
Forms\Components\Section::make(__('gui.notifications.title'))
->schema([
Select::make('power_plant_id')
->default(now()->format('Y'))
->options(fn (User $record): ?array => $record->powerPlants->pluck('name', 'power_plant_id')->toArray())
->label(__('gui.powerplants.title'))
->reactive(),
Checkbox::make('active')->default(true),
CheckboxList::make('notifications')
->options(function (callable $get) {
$userId = $get('UserId');
$powerPlantId = $get('power_plant_id');
$notifications = UserPowerPlant::where('user_id', $userId)->where('power_plant_id', $powerPlantId)->get();
return [
'not_1' => 'All notification',
'not_2' => 'Some notification',
'not_e' => 'No notification',
];
}),
])
->columns(2)
->columnSpan(
['lg' => 3]
),
Forms\Components\Section::make(__('gui.notifications.title'))
->schema([
Select::make('power_plant_id')
->default(now()->format('Y'))
->options(fn (User $record): ?array => $record->powerPlants->pluck('name', 'power_plant_id')->toArray())
->label(__('gui.powerplants.title'))
->reactive(),
Checkbox::make('active')->default(true),
CheckboxList::make('notifications')
->options(function (callable $get) {
$userId = $get('UserId');
$powerPlantId = $get('power_plant_id');
$notifications = UserPowerPlant::where('user_id', $userId)->where('power_plant_id', $powerPlantId)->get();
return [
'not_1' => 'All notification',
'not_2' => 'Some notification',
'not_e' => 'No notification',
];
}),
])
->columns(2)
->columnSpan(
['lg' => 3]
),
Even if I put a dummy checbox - default vallue true dont work
Checkbox::make('active')->default(true),
Checkbox::make('active')->default(true),
These components working only with the Model Value?
Andrew Wallo
Andrew Wallo16mo ago
Look up “dehydrateStateUsing()” in the Filament docs. I honestly don’t know what you are trying to do in the CheckboxList. You arent doing anything with the callable gets and sets… Are you using the form builder or is this in a resource?
Csinesz
CsineszOP16mo ago
in a resource table function I have a select which is reactive - if the user change the select values in the checkbox list de values need to be checked or not depend on the selected values in the select list there are companyies and avery companies has a different nofifications - which I tried to set with a Checkbox list
Andrew Wallo
Andrew Wallo16mo ago
Yeah I don’t think you can call the state of another field within the options array of a CheckboxList
Csinesz
CsineszOP16mo ago
so i cannot able to set the CheckBoxList values? 😦
Andrew Wallo
Andrew Wallo16mo ago
I mean usually when you are doing things with dependent fields you only set a Field’s value not the Field’s options array. There may be a way but I couldn’t tell you right now.
GHosT
GHosT15mo ago
Hi, are you solve this??
Csinesz
CsineszOP15mo ago
Sadly no 😦
pinoooh
pinoooh10mo ago
This works for me:
CheckboxList::make('components')
->options([
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
])
->afterStateHydrated(static function (Set $set, $state): void {
$set('components', ['one', 'two']);
});
CheckboxList::make('components')
->options([
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
])
->afterStateHydrated(static function (Set $set, $state): void {
$set('components', ['one', 'two']);
});
Want results from more Discord servers?
Add your server