zizoujab
zizoujab

Reputation: 7800

Custom validation messages for standard validators in Symfony2

I want to override only one message in symfony2 default validation message which is :

This value should be the user current password.

this message has as a key security.validator.user_password I tried to override this message by creating BundleName/Resources/config/security.yml.

BundleName/Resources/config/security.yml

security:
    validator:
        user_password: custom message 

I tried xml file and cleared the cache but I get nothing.

Any suggestions ?

Upvotes: 0

Views: 421

Answers (2)

Justin Finkelstein
Justin Finkelstein

Reputation: 472

I found a solution to this when using FOSUserBundle as a parent to my Users bundle:

# Acme\UsersBundle\config\validation.yml
Acme\UsersBundle\Entity\User:
    properties:
        password:
            - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
                message: Please check your current password and try again

This pattern can be used to override any of the default strings which are shown when validation on a given field in that entity fails.

So to clarify:

  • Acme\UsersBundle\Entity\User: the Entity on which validation is carried out
  • password: the name of the entity attribute you're validating
  • Symfony\etc: the class which is actually doing the validation
  • message: What you want to override with

Hope this helps!

Upvotes: 0

fitz3012
fitz3012

Reputation: 11

I think you can override using the validation.yml file in your bundle config like said on symfony2 doc.

### src/UserBundle/Resources/config/validation.yml
Acme\UserBundle\Form\Model\ChangePassword:
properties:
   oldPassword:
       - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
           message: "Wrong value for your current password"

Upvotes: 1

Related Questions