Can somebody explain me why UniqueEntity constraint class defines:
// ...
public $service = 'doctrine.orm.validator.unique';
public function validatedBy()
{
return $this->service;
}
//...
And not simply:
public function validatedBy()
{
return "UniqueEntityValidator";
}
UniqueEntityValidator class exists and has all the logic it needs. Whats the point of service here?
I’m asking this because now I cannot use UniqueEntity outside Symfony framework due to some dependencies.
As described in this recipe for the Validator component,
validatedBy()returns an alias, not a service directly. This allows you to configure your ownValidatorservice in the DIC:As this
UniqueEntityconstraint is part of the bridge, it means you should use it in a Symfony2 context. That’s why you can’t really use it outside of the framework.But you can change this
servicevalue to a class name. Attributes are public: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php, and according to the documentation given above, it will work.