Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 565bc52

Browse filesBrowse files
committed
[Validator] Add createValidCallable() that returns a boolean
1 parent f052d11 commit 565bc52
Copy full SHA for 565bc52

File tree

3 files changed

+45
-5
lines changed
Filter options

3 files changed

+45
-5
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/ValidationTest.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@ public function testCreateCallableInvalid()
3333
$this->expectException(ValidationFailedException::class);
3434
$validator('test');
3535
}
36+
37+
public function testCreateIsValidCallableValid()
38+
{
39+
$validator = Validation::createIsValidCallable(new Email());
40+
$this->assertTrue($validator('test@example.com'));
41+
}
42+
43+
public function testCreateIsValidCallableInvalid()
44+
{
45+
$validator = Validation::createIsValidCallable(new Email());
46+
$this->assertFalse($validator('test'));
47+
}
3648
}

‎src/Symfony/Component/Validator/Validation.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Validation.php
+28-5Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ 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+
$violations = new ConstraintViolationList();
35+
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, ConstraintViolationListInterface $violations = null): bool
50+
*/
51+
public static function createIsValidCallable($constraintOrValidator = null, Constraint ...$constraints): callable
3052
{
3153
$validator = $constraintOrValidator;
3254

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

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

42-
return static function ($value) use ($constraints, $validator) {
43-
$violations = $validator->validate($value, $constraints);
44-
if (0 !== $violations->count()) {
45-
throw new ValidationFailedException($value, $violations);
64+
return static function ($value, ConstraintViolationListInterface $violations = null) use ($constraints, $validator) {
65+
$valueViolations = $validator->validate($value, $constraints);
66+
67+
if ($violations) {
68+
$violations->addAll($valueViolations);
4669
}
4770

48-
return $value;
71+
return 0 === $valueViolations->count();
4972
};
5073
}
5174

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.