Symfony2 用户实体没有选择自定义验证器类 [英] Symfony2 User entity not picking up custom validator class

查看:23
本文介绍了Symfony2 用户实体没有选择自定义验证器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义密码验证器,有人在回答另一个时给了我一个问题.验证器看起来像这样:

I have a custom password validator that someone gave me in an answer to another question. The validator looks like this:

<?php
namespace Symfony\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
{
  // ...
}

我尝试使用这个验证器的地方是在我的 User 实体中,它看起来像这样:

The place where I'm trying to use this validator is in my User entity, which looks like this:

<?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 Symfony\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
{
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('current_password', new CurrentPassword());
    }
}

(当然,为了清楚起见,我省略了一些代码.)

(I've omitted some code, of course, for clarity.)

我遇到的问题是我的验证器类没有被识别:

The problem I'm having is that my validator class is not being recognized:

致命错误:在第 438 行的/home/jason/pressbox/src/VNN/PressboxBundle/Entity/User.php 中找不到 Class 'Symfony\Component\Validator\Constraints\CurrentPassword'

Fatal error: Class 'Symfony\Component\Validator\Constraints\CurrentPassword' not found in /home/jason/pressbox/src/VNN/PressboxBundle/Entity/User.php on line 438

为什么会发生这种情况?

Why could this be happening?

推荐答案

您必须自己编写约束.它没有随 Symfony2 一起提供.

You have to write the constraint yourself. It doesn't come with Symfony2.

这篇关于Symfony2 用户实体没有选择自定义验证器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆