9

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

pleerock
  • 18,322
  • 16
  • 103
  • 128

3 Answers3

1

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

Markus
  • 440
  • 2
  • 10
0

Maybe a Custom Validation Constraint could help you if you could (depending of your application logic) remove those two validations and make your own.

Something like this maybe?

http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

edo.schatz
  • 99
  • 4
0

If you're happy to set up at least some of your validation rules via a YAML file rather than annotations, you can override the base class's validation settings without needing to edit the class file itself.

Your YAML file would look something like this and would need to be in a location like src/YourApp/YourBundle/Resources/config/validation.yml to be picked up automatically:

BaseEntity:
    properties:
        name:
            - NotBlank:
                message: user.name.not_blank
            - Length:
                min: 2
                minMessage: user.name.short
                max: 255
                maxMessage: default.name.long

If you want to put your validation file in a non-standard location, see https://stackoverflow.com/a/24210501/328817

Community
  • 1
  • 1
Sam
  • 5,997
  • 5
  • 46
  • 66