F
Filament15mo ago
SLy

Setting default value in input with createOptionForm

The ->default(fn (Get $get): ?int => $get('state_id')) in following code returns $get('state_id') as null, even though I select a value in state_id select, how can I prefill an input in the create createOptionForm modal with the value from another form field?
Forms\Components\Select::make('city_id')
->label('City')
->relationship('city', 'name')
->options(
fn (Get $get) => $get('state_id')
? City::whereStateId($get('state_id'))->pluck('name', 'id')
: []
)
->createOptionForm([
Forms\Components\TextInput::make('state_id')
->default(fn (Get $get): ?int => $get('state_id'))
->required(),
Forms\Components\TextInput::make('name')
->required(),
])
->preload()
->live()
->required(),
]),
Forms\Components\Select::make('city_id')
->label('City')
->relationship('city', 'name')
->options(
fn (Get $get) => $get('state_id')
? City::whereStateId($get('state_id'))->pluck('name', 'id')
: []
)
->createOptionForm([
Forms\Components\TextInput::make('state_id')
->default(fn (Get $get): ?int => $get('state_id'))
->required(),
Forms\Components\TextInput::make('name')
->required(),
])
->preload()
->live()
->required(),
]),
Solution:
That's because the $get in that context is the form in the createOptionForm modal, not the parent form. So you are just getting the same state_id you are trying to set a default on. I don't know if this will work, but you could try something like ... ```php...
Jump to solution
2 Replies
Solution
cheesegrits
cheesegrits15mo ago
That's because the $get in that context is the form in the createOptionForm modal, not the parent form. So you are just getting the same state_id you are trying to set a default on. I don't know if this will work, but you could try something like ...
->createOptionForm(function (Get $get) {
return [
Forms\Components\TextInput::make('state_id')
->default($get('state_id'))
->required(),
Forms\Components\TextInput::make('name')
->required(),
];
}
)
->createOptionForm(function (Get $get) {
return [
Forms\Components\TextInput::make('state_id')
->default($get('state_id'))
->required(),
Forms\Components\TextInput::make('name')
->required(),
];
}
)
SLy
SLyOP15mo ago
Yes, it works, thank you!
Want results from more Discord servers?
Add your server