Skip to content

Commit 36c2961

Browse files
committed
feature #16909 Allows access to payload in callback validator (conradkleinespel)
This PR was merged into the 3.1-dev branch. Discussion ---------- Allows access to payload in callback validator | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15092 | License | MIT | Doc PR | none Commits ------- 26cd91d allows access to payload in callback validator
2 parents e23804e + 26cd91d commit 36c2961

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,28 @@ public function testAnnotationInvocationMultiValued()
226226

227227
$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
228228
}
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+
}
229253
}

0 commit comments

Comments
 (0)