Skip to content

Commit

Permalink
feat: add Translator to SymfonyValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb authored and f3l1x committed May 24, 2024
1 parent 1759ce0 commit f6f7d56
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .docs/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,18 @@ final class UserFilter extends BasicEntity
```

You can override `ConstraintValidatorFactory` on `SymfonyValidator`. If you want to use [custom validation contstraints](https://symfony.com/doc/current/validation/custom_constraint.html) with support of Nette DI,
you should also install [contributte/validator](https://github.com/contributte/validator). Take a look at example.
you should also install [contributte/validator](https://github.com/contributte/validator).
If you want to use translated constraint messages, you can use [contributte/translation](https://github.com/contributte/translation).
Take a look at example.

```yaml
services:
symfonyValidator:
factory: Apitte\Core\Mapping\Validator\SymfonyValidator
setup:
- setConstraintValidatorFactory(Contributte\Validator\ContainerConstraintValidatorFactory())
- setTranslator(@Contributte\Translation\Translator)
- setTranslationDomain('validators')
api:
plugins:
Apitte\Core\DI\Plugin\CoreMappingPlugin:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"nette/http": "^3.2.3",
"psr/log": "^2.0.0 || ^3.0.0",
"symfony/console": "^6.4.0 || ^7.0.0",
"symfony/translation": "^6.4.0 | ^7.0.0",
"symfony/validator": "^6.4.0 || ^7.0.0",
"symfony/yaml": "^6.4.0 || ^7.0.0",
"tracy/tracy": "^2.10.5"
Expand Down
20 changes: 20 additions & 0 deletions src/Core/Mapping/Validator/SymfonyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatorInterface;

class SymfonyValidator implements IEntityValidator
{
Expand All @@ -16,6 +17,10 @@ class SymfonyValidator implements IEntityValidator

private ?ConstraintValidatorFactoryInterface $constraintValidatorFactory = null;

private ?TranslatorInterface $translator = null;

private ?string $translationDomain = null;

public function __construct(?Reader $reader = null)
{
$this->reader = $reader;
Expand All @@ -27,6 +32,16 @@ public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterfac
$this->constraintValidatorFactory = $constraintValidatorFactory;
}

public function setTranslator(TranslatorInterface $translator): void
{
$this->translator = $translator;
}

public function setTranslationDomain(string $translationDomain): void
{
$this->translationDomain = $translationDomain;
}

/**
* @throws ValidationException
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
Expand All @@ -44,6 +59,11 @@ public function validate(object $entity): void
$validatorBuilder->setConstraintValidatorFactory($this->constraintValidatorFactory);
}

if ($this->translator !== null) {
$validatorBuilder->setTranslator($this->translator);
$validatorBuilder->setTranslationDomain($this->translationDomain);
}

$validator = $validatorBuilder->getValidator();

/** @var ConstraintViolationListInterface $violations */
Expand Down
2 changes: 2 additions & 0 deletions tests/Cases/Core/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Toolkit::test(function (): void {
)
setup:
- setConstraintValidatorFactory(Symfony\Component\Validator\ConstraintValidatorFactory())
- setTranslator(Symfony\Component\Translation\Translator(en))
- setTranslationDomain(validators)
api:
plugins:
Apitte\Core\DI\Plugin\CoreMappingPlugin:
Expand Down

0 comments on commit f6f7d56

Please sign in to comment.