user3113427
user3113427

Reputation: 465

How does the symfony groups validation work

I am little bit confused regarding symfony group validation.

Suppose i have this code

 * @NotBlank(groups={"A", "B", "C"})
 */
private $description;

When i submit the form then i am manually inject groups like this

$this->validator->validate($object, groups={"F", "A","C"})

Now i want to know how symfony will do validation

  1. Does symfony check that all the groups ie, F,A,C SHOULD MATCHED WITH A,B,C OR it checks if any groups from F,A,C is present in defined gorup'A,B,C'. So if any item macthes then it do the validation

Upvotes: 0

Views: 111

Answers (2)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

If you take a look at the validate() method signature, you may understand that you should not think of the $groups parameter it as a parameter that allows you to inject validation groups.

It's used to ask your validator to validate a given object against some groups of constraints.

Example of use,

/*
 * @NotBlank(groups={"A", "B"})
 */
private $property1;

/*
 * @NotBlank(groups={"C"})
 */
private $property2;

/*
 * @NotBlank(groups={"B"})
 */
private $property3;

Then,

$this->validator->validate($object, groups={"A", "C"})

will validate your property1 & property2 against the NotBlank constraint.

But when calling,

$this->validator->validate($object, groups={"A", "B"})

only property1 & property3 are validated againt the NotBlank constraint as group C isn't invoked.

Upvotes: 2

maphe
maphe

Reputation: 1931

Symfony lists all constraints from groups F, A, C and apply them on your data. If a constraint is in 2 or more groups, it will be applied only once.

So, about your example, the NotBlank constraint on your $description field should be applied.

Upvotes: 1

Related Questions