Skip to content

Commit 4187d38

Browse files
committed
feature #31466 [Validator] add Validation::createCallable() (janvernieuwe)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Validator] add Validation::createCallable() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This is an initial PR to check/discuss the implementation of a callable validator. If there is interest in merging this, I will gladly update the docs and such. The use case is mainly for validation of console questions, since the default validation has been removed in the latest version and i could not find an easy solution to replace it (if there already is some solution for this, i'm not aware of it) and the question helper uses callables. This small class allows the standard symfony validators to be used in console questions, or any other location that requires a callable validator. Example use case: ```php $io = new SymfonyStyle($input, $output); $required = new CallableValidator([new NotBlank()]); $wsdl = $io->ask('Wsdl location (URL or path to file)', null, $required); ``` As said before, this is by no means the final version, but I would like to know if there is interest in merging this (and receive some feedback about the implementation) before I put any more effort into this. Commits ------- 2e4f2ac322 [Validator] add Validation::createCallable()
2 parents ee5ba53 + 70f111a commit 4187d38

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Exception;
13+
14+
use Symfony\Component\Validator\ConstraintViolationListInterface;
15+
16+
/**
17+
* @author Jan Vernieuwe <[email protected]>
18+
*/
19+
class ValidationFailedException extends RuntimeException
20+
{
21+
private $violations;
22+
private $value;
23+
24+
public function __construct($value, ConstraintViolationListInterface $violations)
25+
{
26+
$this->violations = $violations;
27+
$this->value = $value;
28+
parent::__construct($violations);
29+
}
30+
31+
public function getValue()
32+
{
33+
return $this->value;
34+
}
35+
36+
public function getViolations(): ConstraintViolationListInterface
37+
{
38+
return $this->violations;
39+
}
40+
}

Tests/ValidationTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Validator;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Email;
16+
use Symfony\Component\Validator\Exception\ValidationFailedException;
17+
use Symfony\Component\Validator\Validation;
18+
19+
/**
20+
* @author Jan Vernieuwe <[email protected]>
21+
*/
22+
class ValidationTest extends TestCase
23+
{
24+
public function testCreateCallableValid()
25+
{
26+
$validator = Validation::createCallable(new Email());
27+
$this->assertEquals('[email protected]', $validator('[email protected]'));
28+
}
29+
30+
public function testCreateCallableInvalid()
31+
{
32+
$validator = Validation::createCallable(new Email());
33+
$this->expectException(ValidationFailedException::class);
34+
$validator('test');
35+
}
36+
}

Validation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14+
use Symfony\Component\Validator\Exception\ValidationFailedException;
1415
use Symfony\Component\Validator\Validator\ValidatorInterface;
1516

1617
/**
@@ -20,6 +21,23 @@
2021
*/
2122
final class Validation
2223
{
24+
/**
25+
* Creates a callable chain of constraints.
26+
*/
27+
public static function createCallable(Constraint ...$constraints): callable
28+
{
29+
$validator = self::createValidator();
30+
31+
return static function ($value) use ($constraints, $validator) {
32+
$violations = $validator->validate($value, $constraints);
33+
if (0 !== $violations->count()) {
34+
throw new ValidationFailedException($value, $violations);
35+
}
36+
37+
return $value;
38+
};
39+
}
40+
2341
/**
2442
* Creates a new validator.
2543
*

0 commit comments

Comments
 (0)