Change the text of HTML 5 validation messages in Symfony 2

If you want to change the text of the HTML5 validation messages in your Symfony 2 project, you can do it one of two ways.

The controller method:

$builder->add('email', 'email',array(
    'attr'=>array('oninvalid'=>"setCustomValidity('Would you please enter a valid email?')")
));

Or, the Twig template method:

{{ form_row(form.email, { 'attr': {'oninvalid': "setCustomValidity('Please give me a nice email')"} }) }}

Very helpful, and very easy. Thanks to Carlos Granados at StackOverflow for getting me through that the first time.

Be Sociable, Share!

2 thoughts on “Change the text of HTML 5 validation messages in Symfony 2”

    1. Hi Cassiano,

      I can’t work out if that’s a question or a challenge?

      If it’s a question, I have never had need to use annotations or yaml files to achieve this, but I guess you could pull the text from a yaml file without too much difficulty.

      I’m not a huge fan of annotations – I find them a necessary evil. I’m in the ‘comments should be comments’ camp.

      Chris

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.