Skip to content

Commit 0fd5b95

Browse files
bug symfony#36859 [Validator] allow passing a validator to Validation::createCallable() (nicolas-grekas)
This PR was merged into the 5.1 branch. Discussion ---------- [Validator] allow passing a validator to Validation::createCallable() | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As spotted by @stof in symfony#31466 (comment) Commits ------- 1357cbf [Validator] allow passing a validator to Validation::createCallable()
2 parents 754bba4 + 1357cbf commit 0fd5b95

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Symfony/Component/Validator/Validation.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,21 @@ final class Validation
2323
{
2424
/**
2525
* Creates a callable chain of constraints.
26+
*
27+
* @param Constraint|ValidatorInterface|null $constraintOrValidator
2628
*/
27-
public static function createCallable(Constraint ...$constraints): callable
29+
public static function createCallable($constraintOrValidator = null, Constraint ...$constraints): callable
2830
{
29-
$validator = self::createValidator();
31+
$validator = $constraintOrValidator;
32+
33+
if ($constraintOrValidator instanceof Constraint) {
34+
$constraints = \func_get_args();
35+
$validator = null;
36+
} elseif (null !== $constraintOrValidator && !$constraintOrValidator instanceof ValidatorInterface) {
37+
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a "%s" or a "%s" object, "%s" given.', __METHOD__, Constraint::class, ValidatorInterface::class, get_debug_type($constraintOrValidator)));
38+
}
39+
40+
$validator = $validator ?? self::createValidator();
3041

3142
return static function ($value) use ($constraints, $validator) {
3243
$violations = $validator->validate($value, $constraints);

0 commit comments

Comments
 (0)