SF2:如何自定义表单类型消息?

时间:2014-02-22 12:22:11

标签: regex symfony entity constraints validation

我想知道如何修改ContactType上的错误消息。 它可以直接在Type中吗?

我目前的代码:

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       //...
    }

public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => array(
                new NotBlank(array('message' => 'My custon message.')),
                new Length(array('min' => 2), array('message' => 'My custon message.'))
            ),
            'email' => array(
                new NotBlank(array('message' => 'My custon message.')),
                new Email(array('message' => 'My custon message.'))
            ),
            'subject' => array(
                new NotBlank(array('message' => 'My custon message.')),
                new Length(array('min' => 10), array('message' => 'My custon message.'))
            ),
            'message' => array(
                new NotBlank(array('message' => 'My custon message')),
                new Length(array('min' => 5))
            )
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }

    public function getName()
    {
        return 'contact';
    }
}

谢谢大家的帮助。

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:1)

建议更改断言的消息,但您也可以使用表单类型的invalid_message设置。