8000 [Validator] Added getObject() to ExecutionContextInterface · symfony/symfony@c5629bb · GitHub
[go: up one dir, main page]

Skip to content

Commit c5629bb

Browse files
committed
[Validator] Added getObject() to ExecutionContextInterface
1 parent 9b204c9 commit c5629bb

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ public function getValue()
232232
return $this->value;
233233
}
234234

235+
/**
236+
* {@inheritdoc}
237+
*/
238+
public function getObject()
239+
{
240+
return $this->object;
241+
}
242+
235243
/**
236244
* {@inheritdoc}
237245
*/

src/Symfony/Component/Validator/Context/ExecutionContextInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ public function buildViolation($message, array $parameters = array());
9999
*/
100100
public function getValidator();
101101

102+
/**
103+
* Returns the currently validated object.
104+
*
105+
* If the validator is currently validating a class constraint, the
106+
* object of that class is returned. If it is a validating a property or
107+
* getter constraint, the object that the property/getter belongs to is
108+
* returned.
109+
*
110+
* In other cases, null is returned.
111+
*
112+
* @return object|null The currently validated object or null.
113+
*/
114+
public function getObject();
115+
102116
/**
103117
* Sets the currently validated value.
104118
*

src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,24 @@ public function testValidateFailsIfNoConstraintsAndNoObjectOrArray()
658658
{
659659
$this->validate('Foobar');
660660
}
661+
662+
public function testAccessCurrentObject()
663+
{
664+
$test = $this;
665+
$called = false;
666+
$entity = new Entity();
667+
$entity->firstName = 'Bernhard';
668+
669+
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, &$called) {
670+
$called = true;
671+
$test->assertSame($entity, $context->getObject());
672+
};
673+
674+
$this->metadata->addConstraint(new Callback($callback));
675+
$this->metadata->addPropertyConstraint('firstName', new Callback($callback));
676+
677+
$this->validator->validate($entity);
678+
679+
$this->assertTrue($called);
680+
}
661681
}

0 commit comments

Comments
 (0)
0