Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Override Constraints

I have BaseEntity class:

class BaseEntity
{
    /**
     * The name.
     *
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, unique=true, nullable=false)
     * @Assert\Length(min=2, max=255, minMessage="default.name.short", maxMessage="default.name.long")
     * @Assert\NotBlank(message = "default.name.not_blank")
     */
    private $name;
}

and

class UserEntity extends BaseEntity
{
    /**
     * {@inheritDoc}
     *
     * @Assert\Length(min=2, max=255, minMessage="user.name.short", maxMessage="default.name.long")
     * @Assert\NotBlank(message = "user.name.not_blank")
     */
    private $name;
}

Now, When I post a new UserEntity into the form with long or short name Symfony gives me 2 errors: (for long:)

  • default.name.long
  • user.name.long

But I want it to show only ONE error, so : - user.name.long

e.g. I want to override, but do not add another one

like image 674
pleerock Avatar asked Jun 20 '13 15:06

pleerock


1 Answers

I think what you are looking for a validator group. So you can split up you validation rules into groups.

There is a excellent documentation about this feature:

http://symfony.com/doc/current/validation/groups.html

like image 144
Markus Avatar answered Nov 07 '22 16:11

Markus