Conditionnally hidden field still have value after state updated to null
Hi, I already had this problem for another form and never really understood how I could make it to work properly.
So I have a Toggle which conditionally hide a Select. This select use a relationship for the options. After the toggle state is updated, I hide the Select and set it to null.
With this configuration, I can create the ressource without problem, the Select is null when I want to, etc ...
But, when I update a record of this resource, if I change the Toggle to the state where the Select disappear and save, the value linked to this select in the database remain the one I had before editing the record.
Its like when I change the Toggle state and the Select disappear, the Select value is no longer part of the field UPDATED.
So is it a okay if I use a lifecycle hook in the Edit{Resource} class for testing if the Toggle is off and require to set the Select to null ? Or there is a better way to do that directly in the form definition
2 Replies
If I understand correctly, when you hide the Select element from the form, you're removing the field altogether, which means then that its empty/null state is not passed through when the form gets submitted.
So, there are 2 options when it comes to this in a typical browser-based app:
- set the display property of the element to display:none, but not remove the field altogether
- or remove the field but replace it with another element for the same field name so that it gets passed in the POST
Or I guess set some other non-visible property that you check in your beforeSaving logic.
I believe Filament's visible() and hidden() completely remove the object from the form. Is there another built-in way to simply set display:none? or replace it with a placeholder that still gets submitted in the POST?
Does swapping it out with a Hidden field work, since it can hold a value that gets POSTed? https://filamentphp.com/docs/3.x/forms/fields/hidden