Skip to content

Commit ce145cd

Browse files
committed
feature #42593 [Validator] Add the When constraint and validator (wuchen90)
This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the `When` constraint and validator | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT | Doc PR | symfony/symfony-docs#15722 This constraint allows you to apply constraints validation only if the provided condition is matched. Usage: ```php namespace App\Model; use Symfony\Component\Validator\Constraints as Assert; class Discount { private $type; // 'percent' or 'absolute' /** * `@Assert`\GreaterThan(0) * `@Assert`\When( * expression="this.type == 'percent'", * constraints={`@LessThan`(100, message="The value should be between 0 and 100!")} * ) */ private $value; // ... } ``` See the documentation for details. Commits ------- 9b7bdc9b18 [Validator] Add the When constraint and validator
2 parents a02eaa4 + 1c71023 commit ce145cd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
use Symfony\Component\Translation\Translator;
224224
use Symfony\Component\Uid\Factory\UuidFactory;
225225
use Symfony\Component\Uid\UuidV4;
226+
use Symfony\Component\Validator\Constraints\WhenValidator;
226227
use Symfony\Component\Validator\ConstraintValidatorInterface;
227228
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
228229
use Symfony\Component\Validator\ObjectInitializerInterface;
@@ -1571,6 +1572,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
15711572
if (!class_exists(ExpressionLanguage::class)) {
15721573
$container->removeDefinition('validator.expression_language');
15731574
}
1575+
1576+
if (!class_exists(WhenValidator::class)) {
1577+
$container->removeDefinition('validator.when');
1578+
}
15741579
}
15751580

15761581
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)

Resources/config/validator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\Constraints\EmailValidator;
1818
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1919
use Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator;
20+
use Symfony\Component\Validator\Constraints\WhenValidator;
2021
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
2122
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
2223
use Symfony\Component\Validator\Validation;
@@ -95,6 +96,12 @@
9596
'alias' => NotCompromisedPasswordValidator::class,
9697
])
9798

99+
->set('validator.when', WhenValidator::class)
100+
->args([service('validator.expression_language')->nullOnInvalid()])
101+
->tag('validator.constraint_validator', [
102+
'alias' => WhenValidator::class,
103+
])
104+
98105
->set('validator.property_info_loader', PropertyInfoLoader::class)
99106
->args([
100107
service('property_info'),

0 commit comments

Comments
 (0)