覆盖表单字段错误消息

时间:2013-09-24 11:42:54

标签: php symfony symfony-2.1 symfony-forms

我构建的表单只有一个例外。为了“节省成本”,模型的现有属性将用于这个“新领域”。

我的目标:

  • 更改现有属性的标签
  • 更改现有属性的验证
  • 更改现有属性的错误消息

我研究了如何添加验证并更改标签,但我似乎无法找到设置现有字段的错误消息的位置。

// 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',
    )));
}

字段选项是否有密钥可以让我设置验证错误消息?

1 个答案:

答案 0 :(得分:0)

MinLengthMaxLength已合并到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',
    )));
}
相关问题