Symfony2 Validator消息

时间:2014-10-17 09:10:32

标签: symfony validation

这里我的User.php扩展了FOSUserBundle:

class User extends AbstractUser
{
    /**
     * @var string
     *
     * @Assert\Email(
     *     message = "Please insert a valid email.",
     *     checkMX = true
     *    )
     * @Assert\Regex(
     *     pattern="# #",
     *     match=false,
     *     message="Your mail can't contain spaces."
     * )
     * @Assert\NotBlank(groups={"step1", "step0", "edit"})
     *
     */
    protected $email;

    /**
     * @var string
     *
     * @ORM\Column(name="first_name", type="string", length=255, nullable=false)
     * @Assert\NotBlank(groups={"step1", "edit"})
     * @Assert\Regex(
     *                pattern="#(((\.|,|-| )?[0-9]){7}|hotmail|laposte|yopmail|wanadoo|gmail|yahoo|\.fr|\.co|\.it|\.be|\.de|\.es|\.nl|\.com|\.uk|\.at|\.au|\.ch|@)#",
     *                match=false,
     *                message="Warning: Be sure your ad does not contain an email address or phone number.")
     * @Groups({"whitelabel", "mobile","full","large","small"})
     */
    protected $firstName;

    [...]
}

我的validators.fr.xliff

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="52">
                <source>Your mail can't contain spaces.</source>
                <target>Votre mail ne peut pas contenir d'espaces.</target>
            </trans-unit>
            <trans-unit id="53">
                <source>Please insert a valid email.</source>
                <target>Merci d'insérer un email valide.</target>
            </trans-unit>
            <trans-unit id="100">
                <source>Warning: Be sure your ad does not contain an email address or phone number.</source>
                <target>Attention: Ce champ ne doit pas comporter d'adresse email ou numéro de téléphone.</target>
            </trans-unit>
        </body>
    </file>
</xliff>

当我将邮件放在firstName中时,错误消息正确地用法语翻译,但是当我放入空格或无效邮件时,错误消息是英文的。

任何人都知道为什么?

谢谢!

编辑:其他事情可能有助于理解,当我在文档中进行电子邮件验证时,{{value}}未通过电子邮件转换,我的错误中确实有“{{value}}”消息 - http://symfony.com/doc/current/reference/constraints/Email.html

/**
 * @Assert\Email(
 *     message = "The email '{{ value }}' is not a valid email.",
 *     checkMX = true
 * )
 */
 protected $email;

enter image description here

1 个答案:

答案 0 :(得分:-1)

我终于找到了错误:

问题是我没有在树枝中使用表单错误,而是我自己的循环显示错误。

{% if form.email.vars.errors is not empty %}
    {% for error in  form.email.vars.errors %}
        <li>{{ error.messageTemplate|raw }}</li>
    {% endfor %}
{% endif %}

它缺少trans

{% if form.email.vars.errors is not empty %}
    {% for error in  form.email.vars.errors %}
        <li>{{ error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')|raw }}</li>
    {% endfor %}
{% endif %}