将验证组设置为symfony 2中的嵌入表单

时间:2023-01-15 11:18:25

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

如何在嵌入表单中设置验证组?我有两个实体A和B以及每个实体的形式(FormA,FormB)我在FormB中嵌入FormA

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?

如何从FormB设置FormA的验证组?

2 个解决方案

#1


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.

还在FormB-> setDefaultOptions中定义验证组。它将验证组应用于所有子表单。

    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());
                }
            }
    );
}

#2


1  

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.

在Symfony 2.8中不推荐使用cascade_validation属性,它将在Symfony 3.0中删除。要使用cascade_validation,您应该使用@Assert \ Valid约束。

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

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

附:注意:链接提供Symfony 2.8版本。

#1


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.

还在FormB-> setDefaultOptions中定义验证组。它将验证组应用于所有子表单。

    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());
                }
            }
    );
}

#2


1  

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.

在Symfony 2.8中不推荐使用cascade_validation属性,它将在Symfony 3.0中删除。要使用cascade_validation,您应该使用@Assert \ Valid约束。

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

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

附:注意:链接提供Symfony 2.8版本。