0

I encounter a problem related to the use of the EntityType with the extended and multiple set to true (which generates checkboxes) and then validating each field separately (required or not) using the JqueryValidate plugin.

I'm using the automatic rules set by the class and attributes, so all fields work well but the checkboxes.

This happens because the validator uses the attribute name to set validation rules, but each validation rule is only applied once per name, so only the first checkbox is validated. This combined with the impossibility to change the names for the choices of an EntityType with extended and multiple, makes it impossible to use this configurations together.

CODE

FormType

$builder
    ->add('checkboxes', EntityType::class, [
        'class' => MyClass::class,
        'required' => false,
        'expanded' => true,
        'multiple' => true,
    ]);

Checkboxes rendered

<input type="checkbox" id="appbundle_myclass_checkboxes_1" name="appbundle_myclass[checkboxes][]" value="1">
<input type="checkbox" id="appbundle_myclass_checkboxes_2" name="appbundle_myclass[checkboxes][]" value="2" required="">

QUESTION

Is there a way to change the name attribute of those checkboxes so I can do multiple validations on them?

Or is there a workaround to make the validator work in these cases?

5
  • I finally managed to solved this by creating a custom validation method, attached to the first checkbox. In it I check all required checkboxes and return false if any isn't checked. To style it, I hide a fake checkbox, so this will be the one with the validation and not a real one which would have changed to red if the validator returned false. Commented Sep 23, 2022 at 15:26
  • No idea what you're trying to do or what you're asking. Typically, you set required and out of the group sharing the same name, at least one from the group is required to be checked. If you have some that need to be checked and others that do not, then they should not all be in the same group with the same name. Commented Sep 24, 2022 at 4:13
  • There are multiple checkboxes created by the Symfony form. They have the same name and it seems there is no way to change it, so that comment is my workaround. Commented Sep 26, 2022 at 11:00
  • not familiar with symfony. Are you saying that no matter how big the form, every single checkbox in that entire form will all share the same name? That makes no sense as that's not how checkboxes are supposed to work. Only checkboxes in a grouping share the same name, such as "yes", "no", "maybe" to answer one question, and then another question might also have "yes", "no", "maybe" but this group shares a different name. Otherwise the form's post array will be nonsense and you won't know which answers are for which questions. Commented Sep 27, 2022 at 3:27
  • Not exactly. It does not share the same name for all fields in the form, in this case the "chexkboxes" field represents a entity with a manytomany relation, and in that case, that field has the same name for all its choices. This is supposed to be used inside a select(default), but the extended attribute renders the field as a series of chexkboxes instead of a select. So all those checkboxes will have the same name. In the post request, as every checkbox shares the same name, I get an array of only the selected ones. Commented Sep 27, 2022 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.