F
Filamentβ€’5mo ago
jals65

Remove value when form field is hidden

I have some dependent fields on my form, when for example a checkbox is selected, other field is hidden. The problem is when the field is hidden, it maintains its value.
Solution:
In the end I solved it by setting manual default values. If it is boolean, it is set to false, if it is string it is set to empty string "", etc.
Jump to solution
18 Replies
Matthew
Matthewβ€’5mo ago
The Hidden component will always be hidden What are ou trying to do exactly?
Dennis Koch
Dennis Kochβ€’5mo ago
You need to use one of the mutation methods to clear values that are not present.
jals65
jals65β€’5mo ago
No, I am referring to the ->hidden() method that all fields incorporate to hide them. I use it to make dependent fields in forms, so that a certain field is not shown if another is not selected.
Matthew
Matthewβ€’5mo ago
Can you share some code? It would be easier to debug
jals65
jals65β€’5mo ago
TextInput::make()->hidden(function(callable $get){ $this->checkDependency($get($dependentFieldName)) }) $this->checkDependency() function returns true o false value I need to reset to default value when checkDependency is true
jals65
jals65β€’5mo ago
But how can I know at that moment which fields are hidden? From the hidden function itself, is there no way to reset the value to the default when I check the dependency itself?
Dennis Koch
Dennis Kochβ€’5mo ago
Those that your model has but aren't in the dataset?
jals65
jals65β€’5mo ago
In the end, even though they are hidden, they have the value in the dataset. There is no way to know if that field is hidden.
Dennis Koch
Dennis Kochβ€’5mo ago
You sure? I thought the form data wouldn't be included and the field not updated?
jals65
jals65β€’5mo ago
Yes, the value is saved in the database even though it is hidden. I have checked the protected function mutateFormDataBeforeSave(array $data): array and the hidden fields values are included in $data so I can't know what fields are hidden.
Dennis Koch
Dennis Kochβ€’5mo ago
Okay. But you could apply the same conditions to hide the fields to find out which fields are hidden and then override them with null value.
jals65
jals65β€’5mo ago
It's an option, but I'm duplicating the process of calculating the dependency. And it can be a bit slow if there are many dependencies. Is there a way to do it when I first calculate the dependency in the hidden function to hide it or not depending on the dependency?
Tom Dyer
Tom Dyerβ€’5mo ago
I might not be understanding the issue correctly but could you just use the disabled() option and pass in the same arguments as your hidden? That way, when it's hidden, it's also disabled and values won't be passed with the request πŸ™‚
jals65
jals65β€’5mo ago
But when it is disabled, the value is not deleted, it simply prohibits you from changing it, but the value remains in the same state. What I need is the following: I have a checkbox called X. And another checkbox called Y. When the checkbox named Y is selected, the checkbox named X is shown, but if I don't select the checkbox named Y, the checkbox named X will be hidden. And I need the checkbox named X to change its value to false when it is hidden, because it might have been selected before, and has its value to true.
Solution
jals65
jals65β€’5mo ago
In the end I solved it by setting manual default values. If it is boolean, it is set to false, if it is string it is set to empty string "", etc.
jals65
jals65β€’5mo ago
But it would be nice to have a method to get the default value of that field.
Blackpig
Blackpigβ€’5mo ago
Make the hidden fields disabled too - iirc, disabled form inputs are not submitted