Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jatanbhimani's avatar

Issue in toggle column in filament

I created a toggle column in table and I want on specific condition satisfies then only state must be updated. so i used beforeStateUpdated(), and returned false in it but it didn't worked. So what should i return to stop updating it on being updated.

0 likes
3 replies
jatanbhimani's avatar

@tisuchi PFA the below code ToggleColumn::make('status') ->beforeStateUpdated(function ($record, $state) { if(condition matches) { return false; }

                })
                ->afterStateUpdated(function ($record, $state) {
                    // Notify about attempted status change
                    Notification::make()
                        ->title('Status updated!')
                        ->color('success')
                        ->send();
                }),
tisuchi's avatar

@jatanbhimani BTW, wrap your code by 3 ` to highlight properly. Check details here

I don't think you can simply return false. Instead, I will go for the defensive way first. How about this?


ToggleColumn::make('status')
    ->beforeStateUpdated(function ($record, $state) {
        // Replace the following condition with your actual logic
        if ("condition does not match") {
            // Notify the user that the condition is not met
            Notification::make()
                ->title('Title for condition not met')
                ->body('Some message for the user')
                ->danger()
                ->send();

            // Throw a validation exception to stop the state update
            throw ValidationException::withMessages([
                'status' => 'The status cannot be updated because the specific condition is not met.',
            ]);
        }
    })
    ->afterStateUpdated(function ($record, $state) {
        // Notify about attempted status change
        Notification::make()
            ->title('Status updated!')
            ->color('success')
            ->send();
    });

Please or to participate in this conversation.