Skip to content

Loading Resources

Edit this page

The Validator component uses metadata to validate a value. This metadata defines how a class, array or any other value should be validated. When validating a class, the metadata is defined by the class itself. When validating simple values, the metadata must be passed to the validation methods.

Class metadata can be defined in a configuration file or in the class itself. The Validator component collects that metadata using a set of loaders.

See also

You'll learn how to define the metadata in Metadata.

The StaticMethodLoader

The most basic loader is the StaticMethodLoader. This loader gets the metadata by calling a static method of the class. The name of the method is configured using the addMethodMapping() method of the validator builder:

1
2
3
4
5
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->addMethodMapping('loadValidatorMetadata')
    ->getValidator();

In this example, the validation metadata is retrieved executing the loadValidatorMetadata() method of the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class User
{
    protected string $name;

    public static function loadValidatorMetadata(ClassMetadata $metadata): void
    {
        $metadata->addPropertyConstraint('name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('name', new Assert\Length([
            'min' => 5,
            'max' => 20,
        ]));
    }
}

Tip

Instead of calling addMethodMapping() multiple times to add several method names, you can also use addMethodMappings() to set an array of supported method names.

The File Loaders

The component also provides two file loaders, one to load YAML files and one to load XML files. Use addYamlMapping() or addXmlMapping() to configure the locations of these files:

1
2
3
4
5
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->addYamlMapping('validator/validation.yaml')
    ->getValidator();

Note

If you want to load YAML mapping files, then you will also need to install the Yaml component.

Tip

Just like with the method mappings, you can also use addYamlMappings() and addXmlMappings() to configure an array of file paths.

The AttributeLoader

The component provides an AttributeLoader to get the metadata from the attributes of the class. For example:

1
2
3
4
5
6
7
8
use Symfony\Component\Validator\Constraints as Assert;
// ...

class User
{
    #[Assert\NotBlank]
    protected string $name;
}

To enable the attribute loader, call the enableAttributeMapping() method.

To disable the annotation loader after it was enabled, call disableAttributeMapping().

Using Multiple Loaders

The component provides a LoaderChain class to execute several loaders sequentially in the same order they were defined:

The ValidatorBuilder will already take care of this when you configure multiple mappings:

1
2
3
4
5
6
7
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->enableAttributeMapping()
    ->addMethodMapping('loadValidatorMetadata')
    ->addXmlMapping('validator/validation.xml')
    ->getValidator();

Caching

Using many loaders to load metadata from different places is convenient, but it can slow down your application because each file needs to be parsed, validated and converted into a ClassMetadata instance.

To solve this problem, call the setMappingCache() method of the Validator builder and pass your own caching class (which must implement the PSR-6 interface Psr\Cache\CacheItemPoolInterface):

1
2
3
4
5
6
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    // ... add loaders
    ->setMappingCache(new SomePsr6Cache())
    ->getValidator();

Note

The loaders already use a singleton load mechanism. That means that the loaders will only load and parse a file once and put that in a property, which will then be used the next time it is asked for metadata. However, the Validator still needs to merge all metadata of one class from every loader when it is requested.

Using a Custom MetadataFactory

All the loaders and the cache are passed to an instance of LazyLoadingMetadataFactory. This class is responsible for creating a ClassMetadata instance from all the configured resources.

You can also use a custom metadata factory implementation by creating a class which implements MetadataFactoryInterface. You can set this custom implementation using setMetadataFactory():

1
2
3
4
5
6
use Acme\Validation\CustomMetadataFactory;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->setMetadataFactory(new CustomMetadataFactory(...))
    ->getValidator();

Caution

Since you are using a custom metadata factory, you can't configure loaders and caches using the add*Mapping() methods anymore. You now have to inject them into your custom metadata factory yourself.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version