Last Updated: February 25, 2016
·
3.276K
· limonazzo

Form- Basic Constraints - Symfony2

uses:

use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Null;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\False;
use Symfony\Component\Validator\Constraints\Type;

... class UserType ..
... public function buildForm( ...

$builder->add('name', 'text', array(
'label' => 'Name',
// css
'attr' => array(
    'class' => 'someclass',
    'id' => 'someID'
),
'constraints' => array(

    new Blank(array(
        'message' => 'This value should be blank.')
    ),
    new NotBlank(array(
        'message' => 'This value should not be blank.')
    ),
    new NotNull(array(
        'message' => 'This value should not be null.')
    ),
    new Null(array(
        'message' => 'This value should be null.')
    ),
    new True(array(
        'message' => 'This value should be true.')
    ),
    new False(array(
        'message' => 'This value should be false.')
    ),
    new Type(array( 
        'message' => 'This value should be of type {{ type }}.',
        'type' => 'float ') // * see Type
    ),
),
    )
);