8000 [Validator] Added context object method callback to choice validator · symfony/symfony@dc39bc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc39bc9

Browse files
author
Peter Bouwdewijn
committed
[Validator] Added context object method callback to choice validator
1 parent 98051e9 commit dc39bc9

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function validate($value, Constraint $constraint)
4949
if ($constraint->callback) {
5050
if (!is_callable($choices = array($this->context->getClassName(), $constraint->callback))
5151
&& !is_callable($choices = $constraint->callback)
52+
&& !is_callable($choices = array($this->context->getObject(), $constraint->callback))
5253
) {
5354
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
5455
}

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
*/
@@ -118,6 +123,18 @@ public function testValidChoiceCallbackContextMethod()
118123
$this->assertNoViolation();
119124
}
120125

126+
public function testValidChoiceCallbackContextObjectMethod()
127+
{
128+
// search $this for "objectMethodCallback"
129+
$this->setObject($this);
130+
131+
$constraint = new Choice(array('callback' => 'objectMethodCallback'));
132+
133+
$this->validator->validate('bar', $constraint);
134+
135+
$this->assertNoViolation();
136+
}
137+
121138
public function testMultipleChoices()
122139
{
123140
$constraint = new Choice(array(

0 commit comments

Comments
 (0)
0