How to find out currently checked Checklist boxes?

EDIT FROM THE FUTURE: In retrospect this is related to the inconsistency of results when using $get in different parts of the code - even withing the same field for example you will get a different result for dd($get('cars') when using in ->action() and a different one when using in ->visible(). My guess it's because of different life times they are employed, but this is super frustrating. I don't know how to get the current value of my checkbox list. When I do:
dd($get['cars'])
dd($get['cars'])
I will get a list of keys and values. But I don't know which checkbox is toggled on. how to see the real what-I-actually clicked list of items?
19 Replies
awcodes
awcodes9mo ago
If it’s in the array it’s checked.
Wirkhof
WirkhofOP9mo ago
Well, this is strange, I am getting:
Cannot use object of type Filament\Forms\Get as array
Cannot use object of type Filament\Forms\Get as array
when trying dd($get['cars']) The same dd($get('cars')) doesn't work the same in visible() and in action(). In action it is completely ignored for some reason and show every checkbox available, not only the checked ones:
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('customstuff')
->action(function (Forms\Get $get, Forms\Set $set) {
dd($get('cars')); // doesn't work; will contain even unchecked
})
// ->visible(fn (Get $get): bool => dd($get('cars'))) // works; shows only checked
]),
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('customstuff')
->action(function (Forms\Get $get, Forms\Set $set) {
dd($get('cars')); // doesn't work; will contain even unchecked
})
// ->visible(fn (Get $get): bool => dd($get('cars'))) // works; shows only checked
]),
This inconsistent behavior is super frustrating and not what I would expect. Probably, as I said earlier, something to do with when those methods are being executed - so, perhaps action() is triggered before the checkboxes are even being filled so it shows me everything. And perhaps visible() fires later in the lifecycle when checkboxes are checked correctly based on what is saved in the DB ? It would be cool to have some "await" like possibility so I could use $get('cars') even in action(). Because currently I can't. Or if I can, let me know how. There is nothing on Google how to do that.
jaocero
jaocero9mo ago
I think the action is fired when you click submit then you can get the cars using array $data
->action(function (array $data) {
dd($data['cars'])
})
->action(function (array $data) {
dd($data['cars'])
})
@Wirkhof
Wirkhof
WirkhofOP9mo ago
I am getting undefined array key for some reason, but will check my code one more time.
jaocero
jaocero9mo ago
just dd the $data and you can get the checked values
Wirkhof
WirkhofOP9mo ago
I am getting an empty array [] for dd($data)
jaocero
jaocero9mo ago
then there is no currently check cars
Wirkhof
WirkhofOP9mo ago
No, I have already prechecked stuff that is saved in db and repopulaed and also I checked manually a few things. I can't bring $form in action for some reason as well I am using Checkbox List I can't do even $form in action() as well https://discord.com/channels/883083792112300104/1218076957976170506 I absolutely don't get it why I can't get $data nad $form in action()
jaocero
jaocero9mo ago
Actions\Action::make('import')
->icon('untitledui-download-cloud-01')
->label('Import Notion Database')
->action(function (array $data) {

$notionDatabase = NotionDatabaseService::make($data['workspace_id']);

$notionDatabase->importDatabaseFromNotion();
})
->hidden(fn () => checkNotionToken() == null ? true : false)
->requiresConfirmation()
->modalHeading('Import Your Notion Database')
->modalDescription('Import your Notion database to kickstart your form creation journey.')
->modalSubmitActionLabel('Import Now')
->modalIcon('untitledui-database-03')
->form([
Forms\Components\Select::make('workspace_id')
->label('Workspace')
->options(self::$notionWorkspaceService->getNotionWorkspaces()->pluck('workspace_name', 'workspace_id'))
->required()
->searchable(),
]),
Actions\Action::make('import')
->icon('untitledui-download-cloud-01')
->label('Import Notion Database')
->action(function (array $data) {

$notionDatabase = NotionDatabaseService::make($data['workspace_id']);

$notionDatabase->importDatabaseFromNotion();
})
->hidden(fn () => checkNotionToken() == null ? true : false)
->requiresConfirmation()
->modalHeading('Import Your Notion Database')
->modalDescription('Import your Notion database to kickstart your form creation journey.')
->modalSubmitActionLabel('Import Now')
->modalIcon('untitledui-database-03')
->form([
Forms\Components\Select::make('workspace_id')
->label('Workspace')
->options(self::$notionWorkspaceService->getNotionWorkspaces()->pluck('workspace_name', 'workspace_id'))
->required()
->searchable(),
]),
this is how I usually use the array data maybe there is a misconfiguration idk but array $data is works perfectly fine for me I can get all data from the form itself before saving it the database
Wirkhof
WirkhofOP9mo ago
Well, you have it up top I have my action on a button.
jaocero
jaocero9mo ago
doest matter array $data will get all data from your form can you share your full code?
Wirkhof
WirkhofOP9mo ago
Maybe it's Tabs. I will put it outside of tabs and check there
jaocero
jaocero9mo ago
if you don't share your code how can any other member here solve your problem and help you
Wirkhof
WirkhofOP9mo ago
What are you saying? I am sharing code. I am not available 24/7 every second
jaocero
jaocero9mo ago
bruhh
Wirkhof
WirkhofOP9mo ago
wait thanks for now
jaocero
jaocero9mo ago
you just share this dd($get['cars']) how can anyone to expect to help you if you don't show your full code and analysze what is wrong
Wirkhof
WirkhofOP9mo ago
Here is the code:
class CarResource extends Resource
{
protected static ?string $model = Car::class;

public static function form(Form $form): Form
{
return $form
->extraAttributes(['id' => 'car-form'])
->schema([
Section::make('')
->extraAttributes(['class' => 'car-form-buttons'])
->schema([
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('customstuff')
->action(function (array $data) {
dd($data);
})
])
]),

])
class CarResource extends Resource
{
protected static ?string $model = Car::class;

public static function form(Form $form): Form
{
return $form
->extraAttributes(['id' => 'car-form'])
->schema([
Section::make('')
->extraAttributes(['class' => 'car-form-buttons'])
->schema([
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('customstuff')
->action(function (array $data) {
dd($data);
})
])
]),

])
I should get something, but $data is an empty array after dd, so [].
Nox
Nox9mo ago
why dont you use checkbox list form component ?
Want results from more Discord servers?
Add your server