简体   繁体   中英

Symfony2 - How do i validate the Type directly in the Form?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM