Uwe
Uwe

Reputation: 295

symfony2 using validation groups in form

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?

Upvotes: 11

Views: 24703

Answers (6)

Aistis
Aistis

Reputation: 4063

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');
        },
    ));
}

Upvotes: 0

Marc Juchli
Marc Juchli

Reputation: 2288

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

Upvotes: 1

Harold
Harold

Reputation: 699

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

Upvotes: 3

DarkSideOfTheMoon83
DarkSideOfTheMoon83

Reputation: 712

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!

Upvotes: 12

Jeremy
Jeremy

Reputation: 1908

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'),
    );
}

Upvotes: 13

Neil Katin
Neil Katin

Reputation: 246

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

Upvotes: 11

Related Questions