Skip to content

Commit 02d2769

Browse files
symfonyamlnicolas-grekas
authored andcommitted
[Validator] [Choice] Fix callback option if not array returned
1 parent ebd2270 commit 02d2769

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)