sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

choice_field_mask_widget with required fields #5706

Closed jeroendk closed 4 years ago

jeroendk commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.54.0 3.54.0 The missing Symfony Admin Generator

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:

// SonataAdmin/Form/Type/form_admin_fields.html.twig

 jQuery.each(allFields, function (i, field) {
     const fieldContainer = controlGroupIdFunc(field);
     jQuery(fieldContainer).hide();
     jQuery(fieldContainer).find('[required="required"]').attr('data-required', 'required').removeAttr("required");
});

if (map[val]) {
       jQuery.each(map[val], function (i, field) {
          const fieldContainer = controlGroupIdFunc(field);
          jQuery(fieldContainer).show();
          jQuery(fieldContainer).find('[data-required="required"]').attr("required", "required");
      });
}

I can create a pull request if this solution is acceptable.

n4huel commented 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.

jeroendk commented 4 years ago

@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.

givihuda commented 3 years ago

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.