F
Filament3mo ago
Roy

Revert togglecolumn $state if condition unsatisfied.

I have togglecolumn on table. when
ToggleColumn::make( 'confirmed' )
->label( 'Confirmed' )
->updateStateUsing( function ($record, $state): bool {
$user = auth()->user();

if ($user->hasRole( 'admin' )) {
return $state; // Confirm toggle on success
} else {
throw ValidationException::withMessages( [] );
}

} ),
ToggleColumn::make( 'confirmed' )
->label( 'Confirmed' )
->updateStateUsing( function ($record, $state): bool {
$user = auth()->user();

if ($user->hasRole( 'admin' )) {
return $state; // Confirm toggle on success
} else {
throw ValidationException::withMessages( [] );
}

} ),
if the togglecolumn is currently false, when toggle from false to true, if the condition didn't meet, the toggle stay at false. the problem is if the togglecolumn is currently true, when toggle from true to false, if the condition didnt meet, the toggle will still move to false. if refresh the page, the toggle will show true again. I supposed it should stay at true instead of going to false? Any hints or guidance would be greatly appreciated!
9 Replies
Matthew
Matthew3mo ago
What are you trying to do here, prevent non-admins from switching the toggle?
CodeWithDennis
CodeWithDennis3mo ago
Why not just disable it?
Roy
RoyOP3mo ago
yes, and other condition as well. not necessary non-admins is disabled a better choice in this case?
CodeWithDennis
CodeWithDennis3mo ago
I mean its all up to you, but you can do ->disabled(fn() => auth()->user()->hasRole('admin'))
Roy
RoyOP3mo ago
because the above code able to prevent toggle from false to true, but not able to prevent the opposite. and I want to run notification as well. if disabled, can we set notification like below? I think it would become more complex isnt it?

ToggleColumn::make( 'confirmed' )
->label( 'Confirmed' )
->updateStateUsing( function ($record, $state): bool {
$user = auth()->user();

if ($user->hasRole( 'admin' )) {
return $state; // Confirm toggle on success
} else {
Notification::make()
->title( 'Action Blocked' )
->body( 'You do not have permission to change this status.' )
->color( 'danger' )
->warning()
->send();
throw ValidationException::withMessages( [] );
}

} ),

ToggleColumn::make( 'confirmed' )
->label( 'Confirmed' )
->updateStateUsing( function ($record, $state): bool {
$user = auth()->user();

if ($user->hasRole( 'admin' )) {
return $state; // Confirm toggle on success
} else {
Notification::make()
->title( 'Action Blocked' )
->body( 'You do not have permission to change this status.' )
->color( 'danger' )
->warning()
->send();
throw ValidationException::withMessages( [] );
}

} ),
CodeWithDennis
CodeWithDennis3mo ago
Im not sure if you can exactly "halt" it 🤔 I guess you could use beforeStateUpdated to display a notication and afterStateUpdated to revert it back but this is not a good fix...
Matthew
Matthew3mo ago
If you're doing sometihng complex, I would use an action modal, you'll have better control anyway. Table record adjustments.....🤮
Roy
RoyOP3mo ago
can you confirm on your side it is same behaviour as well? is this consider an issue to be submitted on github?
CodeWithDennis
CodeWithDennis3mo ago
Notifications work for me. Just the resetting doesn't work 😛 Disabling the option would be easier imo.
Want results from more Discord servers?
Add your server