symfony2在表单中使用验证组

时间:2022-10-22 18:22:38

I have a Entity with a property:

我有一个属性的实体:

/**
 * @var string $name
 *
 * @Assert\NotBlank(groups={"foobar"})
 * @ORM\Column(name="name", type="string", length=225, nullable=false)
 */
private $name;

The Form:

表格:

class MyType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => '...',
            'validation_group' => array('foobar'),
        );
    }

    public function getName()
    {
        ...
    }
}

In the Controller I bind the Request and call $form->isValid()

在Controller中我绑定Request并调用$ form-> isValid()

But how to define the validation_group?

但是如何定义validation_group呢?

6 个解决方案

#1


10  

When building the form in the controller, add a 'validation_groups' item to the options array:

在控制器中构建表单时,将“validation_groups”项添加到options数组中:

$form = $this->createFormBuilder($users, array(
    'validation_groups' => array('foobar'),
))->add(...)
;

It is described in the forms page of the symfony2 book: http://symfony.com/doc/current/book/forms.html#validation-groups

它在symfony2书的表单页面中描述:http://symfony.com/doc/current/book/forms.html#validation-groups

#2


13  

I had exactly the same problem. I solved it that way ...

我有完全相同的问题。我这样解决了......

// Entity
$employee = new Employee();

// Form creation
$form = $this->createForm(new EmployeeForm(), $employee, array('validation_groups'=>'registration'));

I hope that helps!

我希望有所帮助!

#3


12  

From inside your FormType class you can define the validation groups associated to that type by setting your default options:

从FormType类内部,您可以通过设置默认选项来定义与该类型关联的验证组:

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('group1', 'group2'),
    );
}

#4


2  

For me, on symfony 2.1, i solved it by adding 'Default' in validation_groups like:

对我来说,在symfony 2.1上,我通过在validation_groups中添加'Default'来解决它,例如:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('Default', 'registration')
    ));
}

#5


1  

I made a little blog post related to this problem: http://marcjuch.li/blog/2013/04/21/how-to-use-validation-groups-in-symfony/

我发了一篇与此问题相关的博文:http://marcjuch.li/blog/2013/04/21/how-to-use-validation-groups-in-symfony/

In this post I’m going to show how to use validation groups in symfony with the example of an order form which should offer the possibility to use separate billing and shipping adresses. This includes 3 steps:

在这篇文章中,我将展示如何使用symfony中的验证组和订单表单的示例,该表单应该提供使用单独计费和发货地址的可能性。这包括3个步骤:

  • Group validation contstraints for the shipping related form fields together
  • 将运输相关表单字段的组验证限制在一起
  • Determine which validation contraints are applied, depending on the checkbox value in the submitted form
  • 确定应用哪些验证约束,具体取决于提交表单中的复选框值
  • Copy data from non-shipping fields to shipping fields if checkbox is not selected
  • 如果未选中复选框,则将数据从非装运字段复制到装运字段

#6


0  

You can also define validation groups dynamically:

您还可以动态定义验证组:

// MyCustomType.php
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => function (FormInterface $form) {
            $data = $form->getData();

            if (Client::TYPE_PERSON == $data->getType()) {
                return array('person');
            }

            return array('company');
        },
    ));
}

#1


10  

When building the form in the controller, add a 'validation_groups' item to the options array:

在控制器中构建表单时,将“validation_groups”项添加到options数组中:

$form = $this->createFormBuilder($users, array(
    'validation_groups' => array('foobar'),
))->add(...)
;

It is described in the forms page of the symfony2 book: http://symfony.com/doc/current/book/forms.html#validation-groups

它在symfony2书的表单页面中描述:http://symfony.com/doc/current/book/forms.html#validation-groups

#2


13  

I had exactly the same problem. I solved it that way ...

我有完全相同的问题。我这样解决了......

// Entity
$employee = new Employee();

// Form creation
$form = $this->createForm(new EmployeeForm(), $employee, array('validation_groups'=>'registration'));

I hope that helps!

我希望有所帮助!

#3


12  

From inside your FormType class you can define the validation groups associated to that type by setting your default options:

从FormType类内部,您可以通过设置默认选项来定义与该类型关联的验证组:

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('group1', 'group2'),
    );
}

#4


2  

For me, on symfony 2.1, i solved it by adding 'Default' in validation_groups like:

对我来说,在symfony 2.1上,我通过在validation_groups中添加'Default'来解决它,例如:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('Default', 'registration')
    ));
}

#5


1  

I made a little blog post related to this problem: http://marcjuch.li/blog/2013/04/21/how-to-use-validation-groups-in-symfony/

我发了一篇与此问题相关的博文:http://marcjuch.li/blog/2013/04/21/how-to-use-validation-groups-in-symfony/

In this post I’m going to show how to use validation groups in symfony with the example of an order form which should offer the possibility to use separate billing and shipping adresses. This includes 3 steps:

在这篇文章中,我将展示如何使用symfony中的验证组和订单表单的示例,该表单应该提供使用单独计费和发货地址的可能性。这包括3个步骤:

  • Group validation contstraints for the shipping related form fields together
  • 将运输相关表单字段的组验证限制在一起
  • Determine which validation contraints are applied, depending on the checkbox value in the submitted form
  • 确定应用哪些验证约束,具体取决于提交表单中的复选框值
  • Copy data from non-shipping fields to shipping fields if checkbox is not selected
  • 如果未选中复选框,则将数据从非装运字段复制到装运字段

#6


0  

You can also define validation groups dynamically:

您还可以动态定义验证组:

// MyCustomType.php
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => function (FormInterface $form) {
            $data = $form->getData();

            if (Client::TYPE_PERSON == $data->getType()) {
                return array('person');
            }

            return array('company');
        },
    ));
}