11

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

But how to define the validation_group?

Uwe
  • 295
  • 1
  • 4
  • 14
  • 2
    What do you mean by `how to define the validation_group`? Do you mean change the value `foobar` dynamically? Or something else? Also, note that it should be `validation_groups` and not `validation_group`. – Matt Sep 06 '11 at 17:39
  • No. I just dont know how the Form Class "MyType" and the $form->isValid() (in the Controller) and the validation_groups work together. isValid has no argument to pass the validation_group... – Uwe Sep 07 '11 at 08:27

6 Answers6

13

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

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('group1', 'group2'),
    );
}
Jeremy
  • 1,908
  • 3
  • 25
  • 38
12

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!

DarkSideOfTheMoon83
  • 702
  • 1
  • 8
  • 17
  • I thought this was going to solve all my problems, but it doesn't remove the required="required" symfony is adding to the fields, but if I remove them it works as I'd expect, given what groups I pass in - very annoying. 1 note, I believe it's meant to be `array('validation_groups' => array("registration", "another_group"))` – Scott Flack Nov 04 '13 at 05:14
11

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

$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

Neil Katin
  • 246
  • 2
  • 2
  • Yes I read the Documentation. But I want to use Form Classes as recomended. And with the createFormBuilder I have to ->add() the form fields. But I want to use my Form class "MyType" – Uwe Sep 07 '11 at 08:25
3

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

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\MyBundle\Entity\MyEntity',
        'validation_groups' => array('Default', 'registration')
    ));
}
Harold
  • 669
  • 1
  • 7
  • 31
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/

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:

  • 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
Marc Juchli
  • 2,240
  • 24
  • 20
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');
        },
    ));
}
Aistis
  • 3,695
  • 2
  • 34
  • 34