# Validator

# Contents

# Setup

Require package:

composer require contributte/validator
1

Register extension:

extensions:
    validator: Contributte\Validator\DI\ValidatorExtension
1
2

# Configuration

The extension tries to provide sane defaults so that in most common cases, it works out-of-the-box without the need for further configuration:

  • validation errors are translated if symfony/translation is installed and configured (see contributte/translation);
  • annotation mapping is enabled as long as the doctrine/annotations package is installed (see nettrine/annotations);
  • mapping cache is stored in %tempDir%/cache/Symfony.Validator by default.

If you're not satisfied with these defaults, you can add or override some of the options:

validator:
    # configure various mapping loaders
    mapping:
        xml: [/path/to/mapping.xml, /path/to/another/mapping.xml]
        yaml: [/path/to/mapping.yml, /path/to/another/mapping.yml]
        methods: [loadValidatorMetadataMethodName]

    # use a different mapping cache implementation
    cache: Symfony\Component\Cache\Adapter\RedisAdapter(@redis.client)

    # configure translator and/or translation domain...
    translation:
        translator: My\Translator
        domain: validator

    # ...or disable translation entirely
    translation:
        translator: false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Usage

This extension exposes a configured implementation of Symfony\Component\Validator\Validator\ValidatorInterface in the dependency injection container:

use Symfony\Component\Validator\Validator\ValidatorInterface;

final class MyServiceThatNeedsToValidateStuff
{
    private ValidatorInterface $validator;

    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }
}
1
2
3
4
5
6
7
8
9
10
11

# Custom constraint validators

If you implement custom constrains and constraint validators, this extension makes sure that all dependencies of the validator constructor are autowired and everything just works:

class VatNumber extends \Symfony\Component\Validator\Constraint {}

class VatNumberValidator extends \Symfony\Component\Validator\ConstraintValidator
{
    private ViesService $vies;

    public function __construct(ViesService $vies) // <-- this dependency is automatically autowired
    {
        $this->vies = $vies;
    }

    public function validate($value,\Symfony\Component\Validator\Constraint $constraint)
    {
        if (!$this->vies->validate($value)) {
            $this->context->buildViolation('Value "{{ value }}" is not a valid EU VAT number.')
                ->setParameter('{{ value }}', $value)
                ->addViolation();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20