Reputation: 3615
Can't seem to get validation groups working. It works for the default group, however I cant seem to figure out how to specify different validation groups in the following:
$errors = $validator->validate($entity);
I've a simple entity I'm testing with:
/**
* Class Login
* @package AppBundle\Entity
*/
class Login
{
/**
* @Assert\NotBlank(
* message="not.blank",
* groups={"Default", "login"}
* )
*
* @Assert\Email(
* message="email",
* groups={"Default", "login"}
* )
*/
public $email;
/**
* @Assert\NotBlank(
* message="not.blank",
* groups={"Default", "login"}
* )
*/
public $password;
}
If I add a parameter like this it complains:
$errors = $validator->validate($entity, 'login');
But there's got to be a way to do this, right?
Upvotes: 2
Views: 1044
Reputation: 96909
Actually, the correct way of using Symfony3 validation groups is to list them as an array:
$errors = $validator->validate($entity, null, ['login']);
Btw, depending on your use case you might not need to set Default
group for each property and use just group Login
instead of login
. Then when you validate the entity with group Default
it'll automatically include assertions with group name equal to the class name which is Login
in your case. For more details see: http://symfony.com/doc/current/validation/groups.html
Upvotes: 1
Reputation: 3615
After digging into the Symfony code I found the following:
File: /vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/ValidatorInterface.php
Line: public function validate($value, $constraints = null, $groups = null);
So if I change this:
$errors = $validator->validate($entity, 'login');
To this:
$errors = $validator->validate($entity, null, 'login');
It works!
Upvotes: 0