How to hide/show a text field depending on the selected value in the select field?
Hello.
I have a category table. The line contains the name and has_close_data fields.
I have a table of courses. On the course creation page you can select a category and closing date. How to hide and show a field depending on the has_close_data category?
Concept:
Select::make('category_id')
->relationship('category', 'name')
->preload()
->searchable()
>live()
->afterStateUpdated(function (?string $state) {
// If the selected category_id select field value has_close_data is true, then we do not hide the close_date field; if it is false, then we hide the close_date field.
})
->required(),
DatePicker::make('close_date'),
5 Replies
along those lines.
On top of what @toeknee said, reference from the docs: https://filamentphp.com/docs/3.x/forms/advanced#conditionally-hiding-a-field
This does not work. If I change the value of Select::make('category_id') then the DatePicker::make('close_date') field does not change. This only works if the entry has already been created and when the page is opened. And not when category_id changes.
@WEBMAS I've just tested it while writing a small tutorial, this worked for me
it's a modified second option from what @toeknee said, his first idea didn't work for me
just keep in mind that my fields are different, in your case it should be Category with close_date instead of Country with needs_vat
Sorry, I made a mistake in the code.
Yes. Everything is working. Thank you all!