Skip to content

Commit 8096a42

Browse files
bug symfony#58615 [Validator] [Choice] Fix callback option if not array returned (symfonyaml)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Validator] [Choice] Fix callback option if not array returned | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#58610 | License | MIT ### Issue When using the Choice validator `callback` option, and the callback method provided does not return an array or iterable, it causes a PHP error. See all details and how to reproduce it in the issue symfony#58610 ### Solution In this PR - Add some checks if the callback method provided returns an array or iterable - Add tests for exception and iterable ________________ **NOTE** : This PR replaces [the old one](symfony#58611) _(I messed up with the branch rebase)_ Commits ------- 02d2769 [Validator] [Choice] Fix callback option if not array returned
2 parents 7f548a3 + 02d2769 commit 8096a42

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function validate($value, Constraint $constraint)
5555
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback.');
5656
}
5757
$choices = $choices();
58+
if (!is_array($choices)) {
59+
throw new ConstraintDefinitionException(sprintf('The Choice constraint callback "%s" is expected to return an array, but returned "%s".', trim($this->formatValue($constraint->callback), '"'), get_debug_type($choices)));
60+
}
5861
} else {
5962
$choices = $constraint->choices;
6063
}

src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function objectMethodCallback()
3939
return ['foo', 'bar'];
4040
}
4141

42+
public static function staticCallbackInvalid()
43+
{
44+
return null;
45+
}
46+
4247
public function testExpectArrayIfMultipleIsTrue()
4348
{
4449
$this->expectException(UnexpectedValueException::class);
@@ -134,6 +139,19 @@ public function testValidChoiceCallbackContextMethod()
134139
$this->assertNoViolation();
135140
}
136141

142+
public function testInvalidChoiceCallbackContextMethod()
143+
{
144+
$this->expectException(ConstraintDefinitionException::class);
145+
$this->expectExceptionMessage('The Choice constraint callback "staticCallbackInvalid" is expected to return an array, but returned "null".');
146+
147+
// search $this for "staticCallbackInvalid"
148+
$this->setObject($this);
149+
150+
$constraint = new Choice(['callback' => 'staticCallbackInvalid']);
151+
152+
$this->validator->validate('bar', $constraint);
153+
}
154+
137155
public function testValidChoiceCallbackContextObjectMethod()
138156
{
139157
// search $this for "objectMethodCallback"

0 commit comments

Comments
 (0)