How to use group sequence in symfony form form?

The idea is to first check if all the required fields have been filled in. If all the required data is provided, check that the entered values ​​are correct. The usual case for a sequence of groups. As always, when I apply new GroupSequence(["Basic", "Strict"])

to the option validation_groups

, it turns out that the form is valid even if all fields are empty. If the value is validation_groups

set to ["Basic", "Strict"]

, the form is validated correctly, but with all restrictions, and this is not what I want. What am I doing wrong?

Here is my code:

class MyType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add("name", null, [
                    "constraints" => new NotBlank(["groups" => ["Basic"]])
                ])
                ->add("phone", MyPhoneType::class, [
                    "constraints" => [
                        new NotBlank(["groups" => ["Basic"]]),
                        new PhoneNumber(["groups" => ["Strict"])
                    ]
                ])
                ->add("email", EmailType::class, [
                    "constraints" => [
                        new NotBlank(["groups" => ["Basic"]]),
                        new Email(["groups" => ["Strict"]]),
                    ],
                ])
            ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            "validation_groups" => new GroupSequence(["Basic", "Strict"])
        ]);
    }
}

      

+3


source to share


1 answer


What am I doing wrong?

This is a known bug in Symfony version 2.8 and has been fixed in newer versions.



But I also haven't seen any documentation that shows what you can use GroupSequence

when setting the parameter validation_groups

.

I would suggest that when using Symfony 2.8 a custom validator that takes care of all the validations is the best choice to handle this kind of situation.

0


source







All Articles