2012-04-01 35 views
1

我有一个自定义的密码验证器,有人给我的答案是another question。验证程序是这样的:Symfony2没有拿起自定义验证器类的用户实体

<?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实体,它看起来像这样的地方:

<?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()); 
    } 
} 

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

,我的验证类是不被认可,我遇到的问题是:

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

为什么会发生这种情况?

回答

1

你必须自己写约束。它不包含Symfony2。

+0

好的。我不知道该怎么做,对不起。我该怎么做? – 2012-04-01 18:48:46

+0

我将代码添加到原始答案中。 – 2012-04-01 18:50:15

+0

甜,谢谢。 (我讨厌SO的评论长度最低限度。) – 2012-04-01 18:52:41

相关问题