[英]Symfony custom validator as service
Trying to follow the custom validator as as service example from the official docs . 尝试遵循官方docs中的自定义验证器即服务示例。 I have defined my validator as a service in my "services.yml":
我已经在“ 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
我不明白的是我得到的错误消息:
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'
. 特别是这部分:
'\\Symfony\\Component\\Validator\\Constraints\\CF\\AppBundle\\Validators\\Constraints\\UniqueInSystem'
。 Why symfony tries to search in this weird namespace? 为什么symfony尝试在这个奇怪的名称空间中搜索? How to avoid this issue?
如何避免这个问题?
The validator itself is applied via /src/CF/AppBundle/Resources/config/validation.yml
: 验证器本身通过
/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: app/console container:debug
输出:
$ 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
// This
CF\AppBundle\Entity\SubSystem:
constraints:
- CF\AppBundle\Validators\Constraints\UniqueInSystem: ~
// Should be
CF\AppBundle\Entity\SubSystem:
constraints:
- CF\AppBundle\Validator\Constraints\UniqueInSystem: ~
It's always been a bit of a mystery as to why the Constraints directory is not called Constraint. 为什么Constraints目录不称为Constraint,一直是一个谜。 But Validator has always been called Validator.
但是Validator一直被称为Validator。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.