F
Filament13mo ago
urbycoz

Is there a way to have "information-only" elements inside a form?

I've made a form with two components (see below). I'd really like to update the client's name on the screen when the user changes the client_id in the select. Section::make('client') ->schema([ Select::make('client_id') ->options( $this->getClientsArray() ) ->label('Client ID'), TextInput::make('client_name')->label(Client Name)->disabled(), ]),
Solution:
You can make your Select field live() and then use the ->afterStateUpdated() method to set the value of the TextInput field (which you can also mark as ->disabled() like you have in your example so that people can't update the value) Take a look here for more on afterStateUpdated() - https://filamentphp.com/docs/3.x/forms/advanced#field-updates and here for more on the different things (including Set, which you'll need for your example) you can inject into the afterStateUpdated() closure parameter - https://filamentphp.com/docs/3.x/forms/advanced#form-component-utility-injection...
Jump to solution
4 Replies
ChesterS
ChesterS13mo ago
You can try a ViewColumn and put w/e you want I guess
Solution
Alex Six
Alex Six13mo ago
You can make your Select field live() and then use the ->afterStateUpdated() method to set the value of the TextInput field (which you can also mark as ->disabled() like you have in your example so that people can't update the value) Take a look here for more on afterStateUpdated() - https://filamentphp.com/docs/3.x/forms/advanced#field-updates and here for more on the different things (including Set, which you'll need for your example) you can inject into the afterStateUpdated() closure parameter - https://filamentphp.com/docs/3.x/forms/advanced#form-component-utility-injection
urbycoz
urbycozOP13mo ago
Aren't they just for tables Thanks. That works great: Section::make('client') ->schema([ Select::make('client_id') ->options( $this->getClientsArray() ) ->label('Client ID'), ->live() ->afterStateUpdated(fn (Set $set, ?string $state) => $set( 'client_name, Client::where('id', '=', $state)->first()->name )) TextInput::make('client_name')->label(Client Name)->disabled() ]),
Alex Six
Alex Six13mo ago
Awesome! Glad it worked out for you!
Want results from more Discord servers?
Add your server