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

[Validator] Add Validation::createIsValidCallable() that returns a boolean instead of exception #40240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3
---

* Add `Validation::createIsValidCallable()` that returns true/false instead of throwing exceptions

5.2.0
-----

Expand Down
26 changes: 24 additions & 2 deletions 26 src/Symfony/Component/Validator/Tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,29 @@ public function testCreateCallableValid()
public function testCreateCallableInvalid()
{
$validator = Validation::createCallable(new Email());
$this->expectException(ValidationFailedException::class);
$validator('test');
try {
$validator('test');
$this->fail('No ValidationFailedException thrown');
} catch (ValidationFailedException $e) {
$this->assertEquals('test', $e->getValue());

$violations = $e->getViolations();
$this->assertCount(1, $violations);
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
}
}

public function testCreateIsValidCallableValid()
{
$validator = Validation::createIsValidCallable(new Email());
$this->assertTrue($validator('test@example.com'));
}

public function testCreateIsValidCallableInvalid()
{
$validator = Validation::createIsValidCallable(new Email());
$this->assertFalse($validator('test', $violations));
$this->assertCount(1, $violations);
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
}
}
29 changes: 24 additions & 5 deletions 29 src/Symfony/Component/Validator/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,30 @@ final class Validation
* Creates a callable chain of constraints.
*
* @param Constraint|ValidatorInterface|null $constraintOrValidator
*
* @return callable($value)
*/
public static function createCallable($constraintOrValidator = null, Constraint ...$constraints): callable
{
$validator = self::createIsValidCallable($constraintOrValidator, ...$constraints);

return static function ($value) use ($validator) {
if (!$validator($value, $violations)) {
throw new ValidationFailedException($value, $violations);
}

return $value;
};
}

/**
* Creates a callable that returns true/false instead of throwing validation exceptions.
*
* @param Constraint|ValidatorInterface|null $constraintOrValidator
*
* @return callable($value, &$violations = null): bool
*/
public static function createIsValidCallable($constraintOrValidator = null, Constraint ...$constraints): callable
{
$validator = $constraintOrValidator;

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

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

return static function ($value) use ($constraints, $validator) {
return static function ($value, &$violations = null) use ($constraints, $validator) {
$violations = $validator->validate($value, $constraints);
if (0 !== $violations->count()) {
throw new ValidationFailedException($value, $violations);
}

return $value;
return 0 === $violations->count();
};
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.