Skip to content

Commit a86583d

Browse files
committed
feature #19745 [Validator] Added context object method callback to choice validator (Peter Bouwdewijn)
This PR was squashed before being merged into the 3.2-dev branch (closes #19745). Discussion ---------- [Validator] Added context object method callback to choice validator | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 835dcf3 [Validator] Added context object method callback to choice validator
2 parents bfe0af5 + 835dcf3 commit a86583d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function validate($value, Constraint $constraint)
4747
}
4848

4949
if ($constraint->callback) {
50-
if (!is_callable($choices = array($this->context->getClassName(), $constraint->callback))
50+
if (!is_callable($choices = array($this->context->getObject(), $constraint->callback))
51+
&& !is_callable($choices = array($this->context->getClassName(), $constraint->callback))
5152
&& !is_callable($choices = $constraint->callback)
5253
) {
5354
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static function staticCallback()
3232
return array('foo', 'bar');
3333
}
3434

35+
public function objectMethodCallback()
36+
{
37+
return array('foo', 'bar');
38+
}
39+
3540
/**
3641
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
3742
*/
@@ -132,6 +137,18 @@ public function testValidChoiceCallbackContextMethod()
132137
$this->assertNoViolation();
133138
}
134139

140+
public function testValidChoiceCallbackContextObjectMethod()
141+
{
142+
// search $this for "objectMethodCallback"
143+
$this->setObject($this);
144+
145+
$constraint = new Choice(array('callback' => 'objectMethodCallback'));
146+
147+
$this->validator->validate('bar', $constraint);
148+
149+
$this->assertNoViolation();
150+
}
151+
135152
public function testMultipleChoices()
136153
{
137154
$constraint = new Choice(array(

0 commit comments

Comments
 (0)