Fill CheckboxList with a default selection

GitHub
filament-companies/APITokens.php at 4350081de511cc237e4163fc9439cca...
Contribute to andrewdwallo/filament-companies development by creating an account on GitHub.
30 Replies
Andrew Wallo
Andrew WalloOP2y ago
Even if I add ->default($this->permissions) which is = to FilamentCompanies::defaultpermissions, which would include "read" as the default permissions, it does not work. This is wanted before the User submits anything and the "read" value does not come from the database, just a string in an array. I should also mention that:
FilamentCompanies::$permissions = ['create', 'read', 'update', 'delete']
FilamentCompanies::$permissions = ['create', 'read', 'update', 'delete']
Which is why it is in the list of options
toeknee
toeknee2y ago
So the default will not work if you are using mountUsing or Fill. So you would need to add it into that method if you are using that to pre-fill some data
Andrew Wallo
Andrew WalloOP2y ago
But the mountUsing is being passed to the edit action not the create action...?
toeknee
toeknee2y ago
Can you provide the code? Just saw the link sorry
Andrew Wallo
Andrew WalloOP2y ago
Okay cool
toeknee
toeknee2y ago
Can you try changing the name of the option from abilities to myabilities using the default() method still, I suspect because of the livewire component you are having overriding values
Andrew Wallo
Andrew WalloOP2y ago
I should mention that in the code I sent please ignore the default value for the tags table column for abilities that is wrong. I didn't add default value yet in the code I sent. So here is an example of what I tried and won't work.
protected function getActions(): array
{
return [
Action::make('create')
->label(trans('Create Token'))
->action(function (array $data) {
$name = $data['name'];
$indexes = $data['abilities'];
$abilities = FilamentCompanies::$permissions;
$selected = collect($abilities)->filter(function ($item, $key) use (
$indexes
) {
return in_array($key, $indexes);
})->toArray();
$this->displayTokenValue(Auth::user()?->createToken($name, FilamentCompanies::validPermissions(array_values($selected))));
$this->tokenCreatedNotification($name);
$this->reset(['name']);
})
->form([
TextInput::make('name')
->label(__('filament-companies::default.labels.token_name'))
->required(),
CheckboxList::make('abilities')
->label(__('filament-companies::default.labels.permissions'))
->required()
->options(FilamentCompanies::$permissions)
->default($this->permissions)
->columns(2),
]),
];
}
protected function getActions(): array
{
return [
Action::make('create')
->label(trans('Create Token'))
->action(function (array $data) {
$name = $data['name'];
$indexes = $data['abilities'];
$abilities = FilamentCompanies::$permissions;
$selected = collect($abilities)->filter(function ($item, $key) use (
$indexes
) {
return in_array($key, $indexes);
})->toArray();
$this->displayTokenValue(Auth::user()?->createToken($name, FilamentCompanies::validPermissions(array_values($selected))));
$this->tokenCreatedNotification($name);
$this->reset(['name']);
})
->form([
TextInput::make('name')
->label(__('filament-companies::default.labels.token_name'))
->required(),
CheckboxList::make('abilities')
->label(__('filament-companies::default.labels.permissions'))
->required()
->options(FilamentCompanies::$permissions)
->default($this->permissions)
->columns(2),
]),
];
}
The default value is read and I even changed the table column name from abilities to permissions there and "read" did show up for the TagsColumn instead of using abilities if I should mention that since it is weird... But if I change everything to be permissions for make('permissions') then I get an error saying arguement haystack is null when trying to click "edit" for a table column even though for the create action it works and shows the correct values in the database
toeknee
toeknee2y ago
You will, so you need to change that to allow null and condition a check if null return empty array
Andrew Wallo
Andrew WalloOP2y ago
But everything I have set works perfectly for the code I have now that I sent in the link but just adding that default value to the checkboxlist component won't show "read" as filled... I think the create action is not pulling from the database and the edit action is.. so it will show null for permissions because in database permissions = abilities
Dan Harrin
Dan Harrin2y ago
what does $this->permissions contain
Andrew Wallo
Andrew WalloOP2y ago
It represents FilamentCompanies::$defaultpermissions which is equal to ['read']
Andrew Wallo
Andrew WalloOP2y ago
Andrew Wallo
Andrew WalloOP2y ago
That is what shows for Livewire serverMemo data On createAction before anything is selected in the checkboxlist
Dan Harrin
Dan Harrin2y ago
isnt it $this->data['permissions']
Andrew Wallo
Andrew WalloOP2y ago
I changed everything that mentions abilities to "permissions" and it would not work for the edit action I guess because in database for sanctum... abilities is used for personal_access_tokens_table and edit action is only reading straight from database so it throws null? But Idk Or maybe the select for permissions is being cached when I open the create modal action?
Andrew Wallo
Andrew WalloOP2y ago
This is what happens on edit action if I change everything to permissions instead of abilities but only when I click on "edit" for the tableaction but not on create and after create the correct values are put in database for sanctum table for permissions
toeknee
toeknee2y ago
Of course it will, adjust it to be: return is_array($state) ? in_array($item, $state) : []; Or check if state is an array before running the filter and convert to an empty array
Andrew Wallo
Andrew WalloOP2y ago
But why would it work with what I have now in the link I sent? for abilities and not permissions
toeknee
toeknee2y ago
I would need to test the code, the error is because there is no array for the haystack so you need to condition it. Essentially editing and creating are slightly different and you need to condition it as such. You are having abilities returning as an array somewhere for it to work
Andrew Wallo
Andrew WalloOP2y ago
Even for the create action though the problem still persists when using permissions because the create action works while edit doesn't, and the tagscolumn for abilities changed to permissions shows blank in table view So regardless I don't think using abilities instead of permissions is the problem here If I added just a string in an array for the default value of checkboxList in the create action connected to its form it should show read as already selected and it doesn't I think Livewire itself treats a checkbox component to cache its selection unless wire:model.defer is used on create
Andrew Wallo
Andrew WalloOP2y ago
Sorry for @ you but I finally figured it out... got me stoked. Just adding the afterStateHydrated() function made it fill in the default value on opening the modal for create... kind of had me mad though for trying for so long haha... didn't even think of doing that just for getting default to work as an option.
toeknee
toeknee2y ago
That makes sense because it’s post hydration of the data being provided. Good stuff
Andrew Wallo
Andrew WalloOP2y ago
Blew my mind, thanks for the help tho
toeknee
toeknee2y ago
Yeah, funny when things get you like that. Essentially if you are filling it should or could be done there. But afterHydration is s nest method too.
Andrew Wallo
Andrew WalloOP2y ago
Is it because it’s opening a modal and messing up the state or something?
toeknee
toeknee2y ago
I doubt it, but my knowledge of livewire is good but not good enough to answer that without testing 🤣
Andrew Wallo
Andrew WalloOP2y ago
Yeah Livewire is so much to learn, plus filament has so many options to learn as well
toeknee
toeknee2y ago
Yeah but at least you have PHPStorm to help 😂
Andrew Wallo
Andrew WalloOP2y ago
Definitely didn’t help me with this but other things yeah haha
toeknee
toeknee2y ago
That’s because it’s more JS based issue here. But at least you solved it’s
Want results from more Discord servers?
Add your server