Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (36 sloc) 1.55 KB

Validator Component

This component is based on the JSR-303 Bean Validation specification and enables specifying validation rules for classes using XML, YAML or annotations, which can then be checked against instances of these classes.

use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintValidatorFactory;

$validator = new Validator(
    new ClassMetadataFactory(new StaticMethodLoader()),
    new ConstraintValidatorFactory()

$constraint = new Assert\Collection(array(
    'name' => new Assert\Collection(array(
        'first_name' => new Assert\MinLength(101),
        'last_name'  => new Assert\MinLength(1),
    'email'    => new Assert\Email(),
    'simple'   => new Assert\MinLength(102),
    'gender'   => new Assert\Choice(array(3, 4)),
    'file'     => new Assert\File(),
    'password' => new Assert\MinLength(60),

$violations = $validator->validateValue($input, $constraint);


Silex integration:

Unit tests:


JSR-303 Specification:

Jump to Line
Something went wrong with that request. Please try again.