13.
Changing Validation Messages
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
7 Comments
Is there a best practice for translated validation messages?
Because in this video you show us to use the string directly:
"This value should not be blank.": Hi! Please enter something for this field :)
`In another video someone recommends using:
user:
email:
`
(and set the validation message as user.email.unique)
Does this differ in terms of performance / best practice?
Hey Mike P.
I'm not sure if using keys impacts performance, but it is recommended to use them, they are short, descriptive, and hard to change.
You can dive deeper about internationalization best practices here: http://symfony.com/doc/curr...
Also symfony has a dedicated section for best practices: http://symfony.com/doc/curr...
I hope it helps you :)
Is it possible to combine the message field with the translation file?
Example:
I want the following error message: "Email cannot be longer than than {{ limit }} characters"
Of course this constraint message is the same for a lot of different fields, just the name "Email" changes.
I want:
Entity:
* @Assert\Length(max=100, maxMessage="Email length.max")
Validators:
length.max: cannot be longer than than {{ limit }} characters
Wanted Result:
Email cannot be longer than than {{ limit }} characters
But this doesn't seem to work.
A TWIG Variable like {{ label }} doesn't seem to exist. (Idea was to add length.max: {{ label }} cannot be longer than than {{ limit }} characters)
Do I really have to generate a translation row inside the validation file for every single label field?
Hey Mike,
That's a good question about customization but a a little overhead. Actually, you don't need field labels in translation messages because errors tied to its fields. Therefore, considering this fact, you already have a context about what exactly field is invalid. So, if you have an HTML layout with a good CSS styles - it should look fine.
Anyway, you *can* override that error message, and you *can* use custom variables in it, so your overwritten message should look like:
{{ label }} cannot be longer than than {{ limit }} characters.
But you need to inject that {{ label }} variable manually in place where this message is translated, for that you need to override a Symfony form template for this field with your own, i.e. create your custom Twig form theme. Here's a bit more information how to create and use your custom Twig form: https://knpuniversity.com/s...
Cheers!
Very profound answer, thank you very much Victor!
Hey guys, I have a weird error, the Symfony profiler toolbar is showing on list page, but not on the new genus page, any idea how I can track this down?!
Hey Shaun T.
Could you show me your controller's code ?
I believe you are missing the head/body tags on your page, does your controller extends from Symfony's base controller ?
Cheers!
"Houston: no signs of life"
Start the conversation!