Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function validate($object, Constraint $constraint)

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

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

if ($reflMethod->isStatic()) {
$reflMethod->invoke(null, $object, $this->context);
$reflMethod->invoke(null, $object, $this->context, $constraint->payload);
} else {
$reflMethod->invoke($object, $this->context);
$reflMethod->invoke($object, $this->context, $constraint->payload);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\CallbackValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Validation;

class CallbackValidatorTest_Class
{
Expand Down Expand Up @@ -227,4 +226,28 @@ public function testAnnotationInvocationMultiValued()

$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
}

public function testPayloadIsPassedToCallback()
{
$object = new \stdClass();
$payloadCopy = null;

$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
'payload' => 'Hello world!',
]);
$this->validator->validate($object, $constraint);
$this->assertEquals('Hello world!', $payloadCopy);

$payloadCopy = null;
$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
]);
$this->validator->validate($object, $constraint);
$this->assertNull($payloadCopy);
}
}