duan02143 2013-04-10 10:38 采纳率: 0%
浏览 62
已采纳

Symfony2 - 如何直接在表单中验证类型?

I'm following this tutorial http://symfony.com/doc/current/book/forms.html#adding-validation

And added this to my form:

    $builder->add('email', null, array('label' =>  'userType.label.email','constraints' => array(new Email())));

This works perfectly fine.

But the following does not work:

    $builder->add('phoneNumber', null, array('label' =>  'userType.label.phoneNumber','required'  => true,'constraints' => array(new Type(array('type' => 'numeric')))));

How can i check if the input is numeric, right in the form?

  • 写回答

1条回答 默认 最新

  • drbe16008 2013-04-10 12:09
    关注

    By this:

    ->add('phoneNumber', 'integer', array('label' =>  'userType.label.phoneNumber','required'  => true));
    

    When you pass null it will use the type you set in the model, is just a way to pass the 3rd parameter without overriding the second one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?