Skip to content

Commit 26cd91d

Browse files
allows access to payload in callback validator
1 parent c3b60a9 commit 26cd91d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function validate($object, Constraint $constraint)
3434

3535
$method = $constraint->callback;
3636
if ($method instanceof \Closure) {
37-
$method($object, $this->context);
37+
$method($object, $this->context, $constraint->payload);
3838
} elseif (is_array($method)) {
3939
if (!is_callable($method)) {
4040
if (isset($method[0]) && is_object($method[0])) {
@@ -43,7 +43,7 @@ public function validate($object, Constraint $constraint)
4343
throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
4444
}
4545

46-
call_user_func($method, $object, $this->context);
46+
call_user_func($method, $object, $this->context, $constraint->payload);
4747
} elseif (null !== $object) {
4848
if (!method_exists($object, $method)) {
4949
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
@@ -52,9 +52,9 @@ public function validate($object, Constraint $constraint)
5252
$reflMethod = new \ReflectionMethod($object, $method);
5353

5454
if ($reflMethod->isStatic()) {
55-
$reflMethod->invoke(null, $object, $this->context);
55+
$reflMethod->invoke(null, $object, $this->context, $constraint->payload);
5656
} else {
57-
$reflMethod->invoke($object, $this->context);
57+
$reflMethod->invoke($object, $this->context, $constraint->payload);
5858
}
5959
}
6060
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Validator\Constraints\Callback;
1616
use Symfony\Component\Validator\Constraints\CallbackValidator;
1717
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18-
use Symfony\Component\Validator\Validation;
1918

2019
class CallbackValidatorTest_Class
2120
{
@@ -227,4 +226,28 @@ public function testAnnotationInvocationMultiValued()
227226

228227
$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
229228
}
229+
230+
public function testPayloadIsPassedToCallback()
231+
{
232+
$object = new \stdClass();
233+
$payloadCopy = null;
234+
235+
$constraint = new Callback([
236+
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
237+
$payloadCopy = $payload;
238+
},
239+
'payload' => 'Hello world!',
240+
]);
241+
$this->validator->validate($object, $constraint);
242+
$this->assertEquals('Hello world!', $payloadCopy);
243+
244+
$payloadCopy = null;
245+
$constraint = new Callback([
246+
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
247+
$payloadCopy = $payload;
248+
},
249+
]);
250+
$this->validator->validate($object, $constraint);
251+
$this->assertNull($payloadCopy);
252+
}
230253
}

0 commit comments

Comments
 (0)