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:Jump to 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...4 Replies
You can try a
ViewColumn
and put w/e you want I guessSolution
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-injectionAren'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()
]),
Awesome! Glad it worked out for you!