skip to Main Content

Yes, hello. I have a question about filament. I’m implementing a search expression select box and toggle within the form method of the resource class. Here, I’m implementing it so that when the toggle button becomes true, the search expression select box becomes null. However, even when I set the toggle button to true and press the save button, the value of the search expression select box is not null, but becomes the previous value. It seems that when I toggle from false to true and back to false, the search expression select box becomes null. I’m really troubled by this. I would appreciate your help in resolving this.

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Select::make('area_id')->label('adminArea')
            ->searchable()
            ->searchDebounce(1000)
            ->getSearchResultsUsing(fn(string $search, ?Building $record) => Area::where('area_code',
                'like',
                "%{$search}%")
                ->whereDoesntHave('Building', fn(Builder $query) => $query->when(!is_null($record),
                    fn(Builder $query) => $query->where('id', '<>', $record?->id)))
                ->limit(50)
                ->get()->mapWithKeys(fn(Area $area
                ) => [$area->id => $area->code_and_name])
            )
            ->getOptionLabelUsing(function (?string $value): ?string {
                return Area::find($value)?->code_and_name;
            })->unique(table: Building::class, column: 'area_id', ignoreRecord: true)
            ->hidden(function (Closure $set, Closure $get) {
                return $get('government_flag') === true;
            }),
            Toggle::make('government_flag')->label('GovernmentFlag')->lazy()
            ->afterStateUpdated(function (Component $component,Closure $set, Closure $get) {
                if ($get('government_flag')) {
                    $set('area_id', null);
                }
            }), 
        ]),
    ]);
}

I commented out hidden() to confirm if the value was becoming null. The value did become null. Additionally, I tried commenting out lazy() function. The behavior of converting to null and the behavior of hidden() have disappeared.

2

Answers


  1. Chosen as BEST ANSWER

    The problem has been resolved on my own.

    There was a post on the official Filament Discord:

    ->hidden() removes the field completely from the form. Whereas a Hidden::make() will create a hidden input. But seeing as this has to do with cost it is best to handle this as Leandro suggested on the backend. Hidden inputs can be manipulated on the front end.

    hidden() removes the select box, causing the value not to be saved. Therefore, the issue was that null values were not being reflected in the database.

    I made the following fix to address this.

    Toggle

    Toggle::make('government_flag')->label('GovernmentFlag')->lazy()
        ->afterStateUpdated(function (Component $component,Closure $set, Closure $get) {
            $building = Building::find($get('id'));
            $area_id = $get('government_flag') ? null : $building?->area_id;
            $set('area_id', $area_id);
        }),
    

    BuildingResource/Pages/EditBuilding.php

    protected function handleRecordUpdate(Model $record, array $data): Model
    {
        $data['area_id'] = $data['area_id'] ?? null;
        
        $record->update($data);
        
        return $record;
    }
    

  2. Can you try removing the if condition inside Toggle column afterStateUpdated method

    Toggle::make('government_flag')
                        ->label('GovernmentFlag')
                        ->reactive()
                        ->afterStateUpdated(function (Component $component, Closure $set, Closure $get) {
                            $set('area_id', null);
                        }),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search