Vesko Vujovic
Vesko Vujovic

Reputation: 400

Dynamic form class constraints doesn't work - Symfony 2.7

This is my form class, i'm submitting request data to this form, for the field "type" this validation work, when i want to create dynamic form constraints are not working. Meaning constraints are not applied on dynamic form.

      class NotesType extends AbstractType
        {
            /**
             * @param FormBuilderInterface $builder
             * @param array $options
             */
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $validation_group = array('groups' => array('note'));

                $builder->add('type', 'text', array(
                    'required' => true,
                    'constraints' => array(new NotBlank($validation_group))
                ));

                $builder->addEventListener(
                    FormEvents::SUBMIT,
                    function (FormEvent $event) {
                        $form = $event->getForm();

                        $formData = $form->getExtraData();

                        if ($formData['type'] === 'note') {
                            $form->add('title', 'text', array(
                                'required' => true,
                                'constraints' => array(
                                    new NotBlank()
                                )))
                                ->add('description', 'text', array(
                                    'required' => true,
                                    'constraints' => array(
                                       new NotBlank()

                                    )));
                        } 

             public function configureOptions(OptionsResolver $resolver)
              {
                    $resolver->setDefaults(array(
                     'data_class' => 'NotesManagerBundle\Entity\Notes',
                     'csrf_protection' => false,
                     'allow_extra_fields' => true,
                      'validation_groups' => array('note')
                    ));
             }

}

Is this problem with validation groups or?

Upvotes: 0

Views: 180

Answers (1)

ste
ste

Reputation: 1539

Yes, you should add the proper validation group event in title and description fields

Upvotes: 0

Related Questions