我在src/VNN/PressboxBundle/Component/Validator/CurrentPasswordValidator.php
中有一个自定义验证器:
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\ConstraintValidator,
Symfony\Component\Validator\Constraint,
Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface,
Symfony\Component\Security\Core\SecurityContextInterface,
JMS\DiExtraBundle\Annotation\Validator,
JMS\DiExtraBundle\Annotation\InjectParams,
JMS\DiExtraBundle\Annotation\Inject;
/**
* @Validator("user.validator.current_password")
*/
class CurrentPasswordValidator extends ConstraintValidator
{
// ...
}
然后我在src/VNN/PressboxBundle/Component/Validator/Contraints/CurrentPassword.php
:
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class CurrentPassword extends Constraint
{
public $message = "Your current password is not valid";
/**
* @return string
*/
public function validatedBy()
{
return 'user.validator.current_password';
}
}
出于某种原因,当我尝试添加使用此验证器的注释时,我收到错误。这是我的实体/注释:
<?php
namespace VNN\PressboxBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinTable as JoinTable;
use Doctrine\ORM\Mapping\JoinColumn as JoinColumn;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\Email;
use VNN\PressboxBundle\Component\Validator\Constraints\CurrentPassword;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* VNN\PressboxBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User implements UserInterface, \Serializable
{
/**
* @var string $password
* @Assert\NotBlank()
* @CurrentPassword()
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
}
我得到的错误是:
AnnotationException:[Semantical Error]注释 “@VNN \ PressboxBundle \分量\验证\ \约束CurrentPassword” 在属性VNN \ PressboxBundle \ Entity \ User :: $ password不存在, 或者无法自动加载。
我做错了什么?
答案 0 :(得分:1)
检查VNN\PressboxBundle\VNNPressboxBundle()
中的AppKernel.php
是否已在内核中正确注册。
更新:如果注册正确,并且src/
已注册为自动装带器的后备(假设VNN/
位于src/
下),则可以想要尝试自己手动加载服务并检查堆栈跟踪是否/何时失败:
$validator = $container->get('user.validator.current_password');
// Pretending this is a unit test:
// $this->assertInstanceOf('VNN\PressboxBundle\Component\Validator\Constraints\CurrentPasswordValidator', $validator);
另外,请确保已清除缓存(如果未在dev
环境中进行测试)。
附注(但与您的问题无关) - 如果您开始添加更多验证程序,使用此语法会更有用:
<?php
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Component\Validator\Constraints as VNN;
class User implements UserInterface, \Serializable
{
/**
* @VNN\CurrentPassword
*/
private $password;
}