Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access current entity in custom symfony2 constraint validator

Tags:

symfony

Is there a way to access the entity of the property currently being validated in a custom constraint validator, and if so, how? As far as i can see, I only have access to the value (and any services I might choose to inject, of course).

like image 312
Eirik A. Johansen Avatar asked Apr 03 '13 11:04

Eirik A. Johansen


2 Answers

In case if you have property validator, you can also access validated object in Validator through ExecutionContext:

http://api.symfony.com/2.8/Symfony/Component/Validator/Context/ExecutionContextInterface.html#method_getObject

class SomeValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        $object = $this->context->getObject();
    }
}
like image 142
ruslanix.com Avatar answered Oct 13 '22 00:10

ruslanix.com


Yes, there is a way. Class constraint validator has an entire entity in scope.

like image 22
gatisl Avatar answered Oct 13 '22 02:10

gatisl