Symfony2 using form validation groups

I have an Entity with the 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 ()

But how to determine validation_group?

+11
validation forms symfony
Sep 06 '11 at 15:35
source share
6 answers

When creating the form in the controller, add the "validation_groups" element to the options array:

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

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

+10
Sep 06 '11 at 17:36
source share

Inside the FormType class, you can define validation groups associated with this type by specifying default parameters:

 public function getDefaultOptions(array $options) { return array( 'data_class' => 'Acme\MyBundle\Entity\MyEntity', 'validation_groups' => array('group1', 'group2'), ); } 
+12
Jan 11 2018-12-01T00:
source share

I had exactly the same problem. I decided it like this ...

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

I hope this helps!

+12
Jan 30 2018-12-12T00:
source share

For me, on symfony 2.1, I solved it by adding "Default" to validation_groups, for example:

 public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Acme\MyBundle\Entity\MyEntity', 'validation_groups' => array('Default', 'registration') )); } 
+2
Nov 08 '12 at 9:18
source share

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

In this post, I am going to show how to use validation groups in symfony with an example order form that should offer the ability to use separate billing and delivery addresses. This includes 3 steps:

  • Group confirmation limits for form fields related to submit.
  • Determine which validation restrictions apply, depending on the value of the flag in the submitted form
  • Copy data from fields without delivery to delivery fields, if the check box is not selected
+1
Apr 21 '13 at 16:35
source share

You can also dynamically define validation groups:

 // 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'); }, )); } 
0
Jul 12 '16 at 12:02
source share



All Articles