14

how to set validation group in embedded forms ? I have two entities A and B and form for each entities (FormA, FormB) I am embedding form FormA in FormB

class FormB extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('A', new FormA());
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TestBundle\Entity\B',
            'validation_groups'=>function(FormInterface $form) {
                // set validation group for FormA here
             },
            'cascade_validation'=>true
        ));
    }
}

How do I set validation group of FormA from FormB?

david
  • 3,225
  • 9
  • 30
  • 43
sonam
  • 3,720
  • 12
  • 45
  • 66

2 Answers2

8

It works for me

class FormB extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('A', new FormA(), array('validation_groups' => array('yourValidationGroup')));
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TestBundle\Entity\B',
            'cascade_validation'=>true
        ));
    }
}

Also defining the validation group in FormB->setDefaultOptions work. It apply the validation group to all subforms.

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TestBundle\Entity\B',
            'cascade_validation'=>true,
            'validation_groups' => array('yourValidationGroup')
        ));
    }

To use that depending on underlying data you can put that code on a event listener:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function(FormEvent $event) {
                $form=$event->getForm();
                /**
                 * @var ContactDetail $detail
                 */
                $entity=$form->getData();
                if($entity->getA()->isRequired()){
                    $form->add('A', new FormA(), array('validation_groups' => array('yourValidationGroup')));
                } else {
                    $form->add('A', new FormA(), array());
                }
            }
    );
}
albert
  • 4,290
  • 1
  • 21
  • 42
  • i m looking to do this in closure for validation groups since i need to add validation group depending on some form element value – sonam Sep 12 '13 at 17:10
  • I add an example that adds a group validation depending on underlying data with EventListeners. – albert Sep 13 '13 at 08:16
  • im trying `$builder->add('A', new FormA(), array('validation_groups' => array('yourValidationGroup')));` and didn't works for me... but putting validation_groups into default options works... Do yo know why first attempt doesnt works? – Pipe Feb 13 '16 at 00:12
2

The property cascade_validation is deprecated in Symfony 2.8, and will be removed in Symfony 3.0. For use cascade_validation, you should use @Assert\Valid constraint.

class Profile
{
  /**
   * @var Address
   * 
   * @Assert\Valid()
   */
  private $address;
}

P.S. Attention: links provides for Symfony 2.8 version.

ZhukV
  • 2,892
  • 6
  • 25
  • 36