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

onairmarc's avatar

Filament Admin Panel Conditional Field Issue

Hi Everybody,

I'm running into an issue with FilamentPHP v3.0.62. The issue is that when a Toggle is true, it no longer triggers the display of a Select field that is conditionally required upon the Toggle being true. However, when I go to save the model, it flashes an error message that triggers the conditionally required Select to display as expected. Placing a value in the conditionally required Select field then allows the Model to save properly. The flash message on the Select field is expected behavior because this field is now required due to the Toggle now being true, but the Select field is never displayed for the user to enter a value prior to clicking save. It previously did dynamically appear when the Toggle had a value of true before saving.

I'm curious if anyone else is encountering this issue and if any fixes have been found. If this truly is a bug, I'll happily submit an issue to the GitHub repo, but I figured I'd ask here first.

Here is the relevant code:

 Forms\Components\Toggle::make('key_contact')
    ->label('Key Contact'),
Forms\Components\Select::make('key_reason_id')
    ->relationship(name: 'keyReason', titleAttribute: 'name')
    ->label('Key Reason')
    ->native(false)
    ->hidden(fn (Get $get) => $get('key_contact') != true)
    ->required(fn (Get $get) => $get('key_contact') == true)
    ->validationAttribute('Key Contact Reason'),

If any clarification is required, please just let me know.

Thanks!

0 likes
3 replies
LaryAI's avatar
Level 58

Based on the provided code snippet, it seems that the issue lies in the usage of the hidden and required methods in the Select field. The hidden method is used to conditionally hide the field based on the value of the key_contact toggle, while the required method is used to conditionally make the field required when the key_contact toggle is true.

To fix the issue, you can try modifying the code as follows:

Forms\Components\Toggle::make('key_contact')
    ->label('Key Contact'),

Forms\Components\Select::make('key_reason_id')
    ->relationship(name: 'keyReason', titleAttribute: 'name')
    ->label('Key Reason')
    ->native(false)
    ->display(fn (Get $get) => $get('key_contact') == true)
    ->required(fn (Get $get) => $get('key_contact') == true)
    ->validationAttribute('Key Contact Reason'),

In the modified code, the hidden method has been replaced with the display method. The display method is used to conditionally show the field based on the value of the key_contact toggle. This ensures that the Select field is displayed to the user when the key_contact toggle is true.

Please note that this solution assumes that the display method is available in the FilamentPHP version you are using. If not, you may need to check the FilamentPHP documentation or consult the FilamentPHP community for alternative solutions.

onairmarc's avatar

The @laryai response does make a good point of working with true values is easier to wrap my head around. I changed the hidden method to be visible as display is not a valid method, but this still does not fix the issue.

Please or to participate in this conversation.