How to Translate Validation Constraint Messages

How to Translate Validation Constraint Messages

The validation constraints used in forms can translate their error messages by creating a translation resource for the validators translation domain.

First of all, install the Symfony translation component (if it’s not already installed in your application) running the following command:

  1. $ composer require symfony/translation

Suppose you’ve created a plain-old-PHP object that you need to use somewhere in your application:

  1. // src/Entity/Author.php
  2. namespace App\Entity;
  3. class Author
  4. {
  5. public $name;
  6. }

Add constraints through any of the supported methods. Set the message option to the translation source text. For example, to guarantee that the $name property is not empty, add the following:

  • Annotations

    1. // src/Entity/Author.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. class Author
    5. {
    6. /**
    7. * @Assert\NotBlank(message="author.name.not_blank")
    8. */
    9. public $name;
    10. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\Author:
    3. properties:
    4. name:
    5. - NotBlank: { message: 'author.name.not_blank' }
  • XML

    1. <!-- config/validator/validation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
    6. https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    7. <class name="App\Entity\Author">
    8. <property name="name">
    9. <constraint name="NotBlank">
    10. <option name="message">author.name.not_blank</option>
    11. </constraint>
    12. </property>
    13. </class>
    14. </constraint-mapping>
  • PHP

    1. // src/Entity/Author.php
    2. namespace App\Entity;
    3. // ...
    4. use Symfony\Component\Validator\Constraints\NotBlank;
    5. use Symfony\Component\Validator\Mapping\ClassMetadata;
    6. class Author
    7. {
    8. public $name;
    9. public static function loadValidatorMetadata(ClassMetadata $metadata)
    10. {
    11. $metadata->addPropertyConstraint('name', new NotBlank([
    12. 'message' => 'author.name.not_blank',
    13. ]));
    14. }
    15. }

Now, create a validators catalog file in the translations/ directory:

  • XML

    1. <!-- translations/validators/validators.en.xlf -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    4. <file source-language="en" datatype="plaintext" original="file.ext">
    5. <body>
    6. <trans-unit id="author.name.not_blank">
    7. <source>author.name.not_blank</source>
    8. <target>Please enter an author name.</target>
    9. </trans-unit>
    10. </body>
    11. </file>
    12. </xliff>
  • YAML

    1. # translations/validators/validators.en.yaml
    2. author.name.not_blank: Please enter an author name.
  • PHP

    1. // translations/validators/validators.en.php
    2. return [
    3. 'author.name.not_blank' => 'Please enter an author name.',
    4. ];

You may need to clear your cache (even in the dev environment) after creating this file for the first time.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.