转换约束将数组转换为字符串转换

时间:2016-03-21 10:44:54

标签: php symfony

我正在尝试翻译validation.yml中定义的自己的约束消息。我有这样的事情:

Adviser\WebServicesBundle\Entity\Messages:
    problem:
            - Type:
                type: string
            - NotBlank: ~
            - Length:
                min: 2
                max: 1000
                minMessage: { message: 'author' }

我需要翻译标签minMessage,如果我直接放置文本,它可以正常工作。我在“资源/翻译”目录中有一个名为validators.es.yml的文件,其中包含翻译消息:

author: "Message too short"

我也证明使用xlf格式添加以下内容覆盖文件validators.es.xlf:

<trans-unit id="81">
            <source>author</source>
            <target>Message too short</target>
</trans-unit>

在我提交表单以强制显示消息时,我在两种情况下都会遇到相同的错误,“数组到字符串转换”。这是抛出异常的代码片段:

     $domain = 'messages';
    }
    $id = (string) $id;
    $catalogue = $this->getCatalogue($locale);
    $locale = $catalogue->getLocale();
    while (!$catalogue->defines($id, $domain)) {

我仔细阅读了此文档但没有成功http://symfony.com/doc/2.7/book/translation.html#translating-constraint-messages

我目前正在使用symfony 2.7,并且在整个项目中任何其他翻译都可以正常工作。我只是有自己的约束消息这个问题,我不明白为什么validation.yml文件无法获取定义的消息。有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

我可以看到您的翻译文件与symfony文档中的文件不同:

<class name="AppBundle\Entity\Author">
    <property name="name">
        <constraint name="NotBlank">
            <option name="message">author.name.not_blank</option>
        </constraint>
    </property>
</class>

因此仅仅为验证消息提供翻译是不够的。

相关问题