F
Filament14mo ago
Pasteko

Select dependant from TextInput

Hello, I'm trying to build a zipcode search inside a form.
TextInput::make('zip_code'),
Select::make('city_id')
->relationship('city', 'name')
->required()
->native(false),
TextInput::make('zip_code'),
Select::make('city_id')
->relationship('city', 'name')
->required()
->native(false),
The user enters the zipcode manually in the text field then the select updates with only the corresponding cities. Tried to follow the various examples for dependant selects but maybe there's a better way with v3?
12 Replies
awcodes
awcodes14mo ago
->searchable()
Pasteko
PastekoOP14mo ago
Thanks, ->searchable() works fine for the select but I have to reproduce the functionality where the user types the zipcode in text field.
awcodes
awcodes14mo ago
Guess I’m missing the point. Sorry. Searchable finds the record and assigns its id to the field.
Pasteko
PastekoOP14mo ago
There's a strange thing happening with ->searchable, when you browse the fields with tab key and you arrive to the select it writes the last key you pressed in the select list.
No description
Pasteko
PastekoOP14mo ago
It's also happening with the demo.
No description
awcodes
awcodes14mo ago
If it’s happening in the demo then it’s a bug. But you should still be able to do what you’re after with the link I provided, regardless of the bug.
Pasteko
PastekoOP14mo ago
I'm trying, thank you.
awcodes
awcodes14mo ago
Totally missed this one sorry. You’ll also need to look into dependent fields and reactivity. https://filamentphp.com/docs/3.x/forms/advanced
Pasteko
PastekoOP14mo ago
https://filamentphp.com/docs/3.x/forms/advanced#dependant-select-options I'm close to something. It's working but I need the first value to be displayed in the select, now I have to click to see all the values.
TextInput::make('zip_code')
->live(),
Select::make('city_id')
->options(fn (Get $get): Collection => ZipCode::query()
->where('code', $get('zip_code'))
->pluck('city', 'id'))
->required()
->native(false),
TextInput::make('zip_code')
->live(),
Select::make('city_id')
->options(fn (Get $get): Collection => ZipCode::query()
->where('code', $get('zip_code'))
->pluck('city', 'id'))
->required()
->native(false),
awcodes
awcodes14mo ago
Sorry, I want to help more but I’m brain dead right now. Working on something really difficult all day, trying to do the impossible.
Pasteko
PastekoOP14mo ago
No problem, your help was already precious. Maybe I have to use ->afterStateUpdated with the TextInput, is it possible to set the select options with?
Want results from more Discord servers?
Add your server