Reputation: 603
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.
Upvotes: 1
Views: 1223
Reputation: 5519
As described in this recipe for the Validator component, validatedBy()
returns an alias, not a service directly. This allows you to configure your own Validator
service in the DIC:
services:
validator.unique.your_validator_name:
class: Fully\Qualified\Validator\Class\Name
tags:
- { name: validator.constraint_validator, alias: doctrine.orm.validator.unique }
As this UniqueEntity
constraint 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 service
value 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.
Upvotes: 5