Can’t customize FOSUserBundle error messages (How to customize Symfony validation messages)
Symfony: v2.7.15
Using theĀ FOSUserBundle, I needed to override the invalid current password error message given when a user edits their profile.
To customize FOSUserBundle’s English messages, you can override these files:
vendor\friendsofsymfony\user-bundle\Resources\translations\FOSUserBundle.en.yml
vendor\friendsofsymfony\user-bundle\Resources\translations\validators.en.yml
…by putting new ones here, and editing them as you see fit:
app\Resources\FOSUserBundle\translations\FOSUserBundle.en.yml
app\Resources\FOSUserBundle\translations\validators.en.yml
Checking in FOSUserBundle’s validators.en.yml, I saw this:
1 2 3 |
# vendor\friendsofsymfony\user-bundle\Resources\translations\validators.en.yml current_password: invalid: 'The entered password is invalid' |
It looked like I might be able to edit the invalid current password message here, but when using the Edit Profile form (edit_content.html.twig), I got a different error message:
“This value should be the user’s current password.”
I found that error existing in this Symfony file:
vendor\symfony\symfony\src\Symfony\Component\Validator\Resources\translations\validators.en.xlf
So, how to override it? Credit goes to Geert Van Damme for providing the information I needed.
Create a validators.en.yml file in your app folder like so:
app\Resources\translations\validators.en.yml
Then, override the message by adding its exact text as the key in your YAML file. For example, to override the password error message mentioned above, you could add this to your validators.en.yml file:
1 2 |
# app\Resources\translations\validators.en.yml This value should be the user's current password.: Invalid password |