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

Skip to content
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 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 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('[email protected]'));
}

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 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