Closed jeroendk closed 4 years ago
Hi @jeroendk, this FormType
only hides and shows the mapped fields. So, be aware that their values will be submitted too. Depends on your case of use, it can be a problem.
@n4huel Yes I know it does for me this is not a problem. If one of the hidden fields is required (and empty) at the moment you are not able to submit the form at all.
Is there maybe a better solution for hiding conditionally-required fields? I'm working on a Sonata 3 admin where there is a drop-down select field of 'Yes' or 'No' attached (mapped?) to a hidden-by-default alphanumeric required text field which will show when 'Yes' is selected, but if that text field is empty during submit (whether it's shown or not), validation will show an error on the empty field; this means that if 'No' is selected, the text field is hidden, and the user won't see any error because it only shows on the field itself.
There's another drop-down that works the same way, but mapped to a numeric field which defaults to 0 and doesn't get the validation error. If I put 0 into the other text field, it doesn't get the validation error (whether 'Yes' or 'No' is selected).
While I can always show the text field, that doesn't prevent the validation error when the field is empty. It must only have its validation checked when 'Yes' is selected; I'm not finding a way to do this in Sonata Admin yet.
Environment
Sonata packages
Subject
Fields stay required while hidden by choice_field_mask_widget
Steps to reproduce
Add one or more required fields and hide them using the choice_field_mask_widget
Expected results
Form is allowed to submit when fields are hidden and empty
Actual results
Form won't submit due to html5 validation.
Is this behaviour known/expected? If not a possible solutions is to remove and add the required attribute on hiding/showing:
I can create a pull request if this solution is acceptable.