8000 feature #40240 [Validator] Add Validation::createIsValidCallable() th… · symfony/symfony@6c0102c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c0102c

Browse files
committed
feature #40240 [Validator] Add Validation::createIsValidCallable() that returns a boolean instead of exception (wouterj)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Validator] Add Validation::createIsValidCallable() that returns a boolean instead of exception | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #36820 | License | MIT | Doc PR | tbd This adds a `Validator::createValidCallable()` (I'm very open for other name suggestions) that returns a boolean instead of exceptions. This allows usingit in places where booleans are expected, for instance in the referenced OptionsResolver case: ```php $resolver->setAllowedValues('name', Validation::createValidCallable( new Assert\Length(['min' => 10 ]) )); ``` Commits ------- e731f5f [Validator] Add createValidCallabl 10000 e() that returns a boolean
2 parents c487a56 + e731f5f commit 6c0102c

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3
5+
---
6+
7+
* Add `Validation::createIsValidCallable()` that returns true/false instead of throwing exceptions
8+
49
5.2.0
510
-----
611

src/Symfony/Component/Validator/Tests/ValidationTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,29 @@ public function testCreateCallableValid()
3030
public function testCreateCallableInvalid()
3131
{
3232
$validator = Validation::createCallable(new Email());
33-
$this->expectException(ValidationFailedException::class);
34-
$validator('test');
33+
try {
34+
$validator('test');
35+
$this->fail('No ValidationFailedException thrown');
36+
} catch (ValidationFailedException $e) {
37+
$this->assertEquals('test', $e->getValue());
38+
39+
$violations = $e->getViolations();
40+
$this->assertCount(1, $violations);
41+
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
42+
}
43+
}
44+
45+
public function testCreateIsValidCallableValid()
46+
{
47+
$validator = Validation::createIsValidCallable(new Email());
48+
$this->assertTrue($validator('test@example.com'));
49+
}
50+
51+
public function testCreateIsValidCallableInvalid()
52+
{
53+
$validator = Validation::createIsValidCallable(new Email());
54+
$this->assertFalse($validator('test', $violations));
55+
$this->assertCount(1, $violations);
56+
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
3557
}
3658
}

src/Symfony/Component/Validator/Validation.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,30 @@ final class Validation
2525
* Creates a callable chain of constraints.
2626
*
2727
* @param Constraint|ValidatorInterface|null $constraintOrValidator
28+
*
29+
* @return callable($value)
2830
*/
2931
public static function createCallable($constraintOrValidator = null, Constraint ...$constraints): callable
32+
{
33+
$validator = self::createIsValidCallable($constraintOrValidator, ...$constraints);
34+
35+
return static function ($value) use ($validator) {
36+
if (!$validator($value, $violations)) {
37+
throw new ValidationFailedException($value, $violations);
38+
}
39+
40+
return $value;
41+
};
42+
}
43+
44+
/**
45+
* Creates a callable that returns true/false instead of throwing validation exceptions.
46+
*
47+
* @param Constraint|ValidatorInterface|null $constraintOrValidator
48+
*
49+
* @return callable($value, &$violations = null): bool
50+
*/
51+
public static function createIsValidCallable($constraintOrValidator = null, Constraint ...$constraints): callable
3052
{
3153
$validator = $constraintOrValidator;
3254

@@ -39,13 +61,10 @@ public static function createCallable($constraintOrValidator = null, Constraint
3961

4062
$validator = $validator ?? self::createValidator();
4163

42-
return static function ($value) use ($constraints, $validator) {
64+
return static function ($value, &$violations = null) use ($constraints, $validator) {
4365
$violations = $validator->validate($value, $constraints);
44-
if (0 !== $violations->count()) {
45-
throw new ValidationFailedException($value, $violations);
46-
}
4766

48-
return $value;
67+
return 0 === $violations->count();
4968
};
5069
}
5170

0 commit comments

Comments
 (0)
0