Trying to follow the custom validator as as service example from the official docs . I have defined my validator as a service in my "services.yml":
cf.validator.unique.in.system:
class: 'CF\AppBundle\Validator\Constraints\UniqueInSystemValidator'
arguments: ['@doctrine']
tags:
- { name: 'validator.constraint_validator', alias: 'cf.validator.unique.in.system' }
I have created the neccesary constraint and validator classes:
constraint
namespace CF\AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class UniqueInSystem extends Constraint
{
public $message = 'This value is already used.';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
public function validatedBy()
{
return 'cf.validator.unique.in.system';
}
}
validator
namespace CF\AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
/**
* @Annotation
*/
class UniqueInSystemValidator extends ConstraintValidator
{
protected $em;
public function __construct(Doctrine $doctrine)
{
$this->em = $doctrine->getManager();
}
public function validate($subsystem, Constraint $constraint)
{
1==1; // here is a breakpoint by now
// $this->context->addViolationAt('[expiresAt].expiresAt', $constraint->message, ['%string%' => $value->format('Y-m-d')] );
}
What I do not understand, is the error message that I get:FatalErrorException: Error: Class '\Symfony\Component\Validator\Constraints\CF\AppBundle\Validators\Constraints\UniqueInSystem' not found in /var/www/escritorio.dev/application/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php line 89
Especially this part: '\Symfony\Component\Validator\Constraints\CF\AppBundle\Validators\Constraints\UniqueInSystem'. Why symfony tries to search in this weird namespace? How to avoid this issue?
UPD
The validator itself is applied via /src/CF/AppBundle/Resources/config/validation.yml:
CF\AppBundle\Entity\SubSystem:
constraints:
- CF\AppBundle\Validators\Constraints\UniqueInSystem: ~
properties:
name:
- NotBlank: ~
- Length:
max: 100
owner:
- NotBlank: ~
leader:
- NotBlank: ~
system:
- NotBlank: ~
status:
- NotBlank : ~
- Type:
type: integer
The app/console container:debug output:
$ php app/console container:debug | grep CF
auc_helper container CF\AppBundle\Services\AucHelper
cf.validator.unique.in.system container CF\AppBundle\Validator\Constraints\UniqueInSystemValidator
cf.validator.unique.in.systemis properly injected to the container? If symfony cannot find validator by alias it will try to include class with the provided validator name.app/console container:debugand I see it loaded