Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abdedda

Browse files
committed
[Validator] Add createValidCallable() that returns a boolean
1 parent f052d11 commit abdedda

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-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('[email protected]'));
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: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ final class Validation
2727
* @param Constraint|ValidatorInterface|null $constraintOrValidator
2828
*/
2929
public static function createCallable($constraintOrValidator = null, Constraint ...$constraints): callable
30+
{
31+
$validator = self::createIsValidCallable($constraintOrValidator, ...$constraints);
32+
33+
return static function ($value) use ($validator) {
34+
if (!$validator($value, $violations)) {
35+
throw new ValidationFailedException($value, $violations);
36+
}
37+
38+
return $value;
39+
};
40+
}
41+
42+
/**
43+
* Creates a callable that returns true/false instead of throwing validation exceptions.
44+
*
45+
* @param Constraint|ValidatorInterface|null $constraintOrValidator
46+
*
47+
* @return callable($value, ConstraintViolationListInterface $violations = null): bool
48+
*/
49+
public static function createIsValidCallable($constraintOrValidator = null, Constraint ...$constraints): callable
3050
{
3151
$validator = $constraintOrValidator;
3252

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

4060
$validator = $validator ?? self::createValidator();
4161

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

48-
return $value;
65+
return 0 === $violations->count();
4966
};
5067
}
5168

0 commit comments

Comments
 (0)