Symfony2 API
Class

Symfony\Component\Validator\ValidatorFactory

class ValidatorFactory implements ValidatorContextInterface

Creates and configures new validator objects

Usually you will use the static method buildDefault() to initialize a
factory with default configuration. To this method you can pass various
parameters that configure where the validator mapping is found. If you
don't pass a parameter, the mapping will be read from annotations.

<code>
// read from annotations only
$factory = ValidatorFactory::buildDefault();

// read from XML and YAML, suppress annotations
$factory = ValidatorFactory::buildDefault(array(
'/path/to/mapping.xml',
'/path/to/other/mapping.yml',
), false);
</code>

You then have to call getValidator() to create new validators.

<code>
$validator = $factory->getValidator();
</code>

When manually constructing a factory, the default configuration of the
validators can be passed to the constructor as a ValidatorContextInterface
object.

<code>
$defaultContext = new ValidatorContext();
$defaultContext->setClassMetadataFactory($metadataFactory);
$defaultContext->setConstraintValidatorFactory($validatorFactory);
$factory = new ValidatorFactory($defaultContext);

$form = $factory->getValidator();
</code>

You can also override the default configuration by calling any of the
methods in this class. These methods return a ValidatorContextInterface object
on which you can override further settings or call getValidator() to create
a form.

<code>
$form = $factory
->setClassMetadataFactory($customFactory);
->getValidator();
</code>

ValidatorFactory instances should be cached and reused in your application.

Methods

static ValidatorFactory buildDefault(array $mappingFiles = array(), Boolean $annotations = false, string $staticMethod = null)

Builds a validator factory with the default mapping loaders

__construct(ValidatorContextInterface $defaultContext = null)

Sets the given context as default context

ValidatorContextInterface setClassMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)

Sets the class metadata factory used in the new validator

ValidatorContextInterface setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)

Sets the constraint validator factory used in the new validator

ValidatorInterface getValidator()

Creates a new validator with the settings stored in this context

Details

at line 110
static public ValidatorFactory buildDefault(array $mappingFiles = array(), Boolean $annotations = false, string $staticMethod = null)

Builds a validator factory with the default mapping loaders

Parameters

array $mappingFiles A list of XML or YAML file names where mapping information can be found. Can be empty.
Boolean $annotations Whether to use annotations for retrieving mapping information
string $staticMethod The name of the static method to use, if static method loading should be enabled

Return Value

ValidatorFactory The validator factory.

Exceptions

MappingException If any of the files in $mappingFiles has neither the extension ".xml" nor ".yml" nor ".yaml"
RuntimeException If annotations are not supported.

at line 173
public __construct(ValidatorContextInterface $defaultContext = null)

Sets the given context as default context

Parameters

ValidatorContextInterface $defaultContext A preconfigured context

at line 191
public ValidatorContextInterface setClassMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)

Sets the class metadata factory used in the new validator

Parameters

ClassMetadataFactoryInterface $metadataFactory The new factory instance

Return Value

ValidatorContextInterface The preconfigured form context

at line 211
public ValidatorContextInterface setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)

Sets the constraint validator factory used in the new validator

Parameters

ConstraintValidatorFactoryInterface $validatorFactory The new factory instance

Return Value

ValidatorContextInterface The preconfigured form context

at line 228
public ValidatorInterface getValidator()

Creates a new validator with the settings stored in this context

Return Value

ValidatorInterface The new validator