Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

148
Vistas
How to use group sequence on form type in symfony forms?

The idea is to first validate if all the required fields are not blank. If all the required data is provided then validate if the values entered are correct. The usual case for groups sequence. How ever when I apply new GroupSequence(["Basic", "Strict"]) to the validation_groups option it turns out the form to be valid even if all the fields are blank. If validation_groups value is set to ["Basic", "Strict"] the form is validated properly but with all constraints, and that 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"])
        ]);
    }
}
8 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

What am I doing wrong?

This is a known bug of Symfony version 2.8 and is fixed newer versions.

But I also did not see any documentation which shows that you can use a GroupSequence when configue the validation_groups option.

I would assume that when using Symfony 2.8 a custom validator which takes care of all validation is the best choice of handling such a situation.

8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos