Php 覆盖表单字段错误消息

Php 覆盖表单字段错误消息,php,symfony,symfony-2.1,symfony-forms,Php,Symfony,Symfony 2.1,Symfony Forms,我创建的表单只有一个例外。为了“节省成本”,该模型的现有财产将用于该“新领域” 我的目标: 更改现有特性的标签 更改现有属性的验证 更改现有属性的错误消息 我研究了如何添加验证和更改标签,但似乎找不到在哪里设置现有字段的错误消息 // MyFormType.php <?php if (!empty($options['unique']) && $options['unique'] == 'some_variable') { $event->getForm(

我创建的表单只有一个例外。为了“节省成本”,该模型的现有财产将用于该“新领域”

我的目标:

  • 更改现有特性的标签
  • 更改现有属性的验证
  • 更改现有属性的错误消息
我研究了如何添加验证和更改标签,但似乎找不到在哪里设置现有字段的错误消息

// MyFormType.php
<?php
if (!empty($options['unique']) && $options['unique'] == 'some_variable') {
    $event->getForm()->add($factory->createNamed('existing_field', 'number', null, array(
        'label'      => 'Number field',
        'required'   => true,
        'max_length' => 6,
        'constraints' => array(
            new MinLength(6),
            new MaxLength(6),
        ),
    )));
} else {
    $event->getForm()->add($factory->createNamed('existing_field', 'text', null, array(
        'label' => 'Text field',
    )));
}
//MyFormType.php

MinLength
MaxLength
在2.3中被合并成了
Length
,我没有2.1或2.2的版本,但我检查了旧的2.1和2.2 API,这应该适合您

// MyFormType.php
<?php
if (!empty($options['unique']) && $options['unique'] == 'some_variable') {
    $event->getForm()->add($factory->createNamed('existing_field', 'number', null, array(
        'label'       => 'Number field',
        'required'    => true,
        'max_length'  => 6,
        'constraints' => array(
            new MinLength(array(
                'limit'   => 6,
                'message' => 'This field must be atleast 6 characters long'
            )),
            new MaxLength(array(
                    'limit'   => 6,
                    'message' => 'This field must be shorter than 6 characters'
                )),
        ),
    )));
} else {
    $event->getForm()->add($factory->createNamed('existing_field', 'text', null, array(
        'label' => 'Text field',
    )));
}
//MyFormType.php

实际上,
Length
似乎是在2.1中添加的。我将该类与您建议的消息一起使用,它现在可以工作了。