0

私が作成したフォームには、1 つの例外があります。「コストを節約する」ために、モデルの既存のプロパティがこの「新しいフィールド」に使用されます。

私の目標:

  • 既存のプロパティのラベルを変更する
  • 既存のプロパティの検証を変更する
  • 既存のプロパティのエラー メッセージを変更する

検証を追加してラベルを変更する方法を考え出しましたが、既存のフィールドのエラー メッセージを設定する場所が見つからないようです。

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

検証エラー メッセージを設定できるフィールド オプションのキーはありますか?

4

1 に答える 1

0

MinLength2.3にMaxLength統合されたばかり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',
    )));
}
于 2013-09-24T22:06:14.523 に答える