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:Jump to 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.
18 Replies
The Hidden component will always be hidden
What are ou trying to do exactly?
You need to use one of the mutation methods to clear values that are not present.
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.
Can you share some code? It would be easier to debug
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
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?
Those that your model has but aren't in the dataset?
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.
You sure? I thought the form data wouldn't be included and the field not updated?
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.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.
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?
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 π
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
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.
But it would be nice to have a method to get the default value of that field.
Make the hidden fields disabled too - iirc, disabled form inputs are not submitted