Symfony2:使用自定义约束进行验证会引发异常

时间:2012-09-19 09:20:41

标签: forms symfony validation

我有一个User类,其中包含一些字段的自定义验证。当我提交表单时,我收到了以下错误:

  

警告:class_parents()[function.class-parents]:类有   不存在,无法加载   .... /供应商/教义/ LIB /学说/ ORM /映射/ ClassMetadataFactory.php   第223行

有人能告诉我我做错了吗?

这是我的用户类:

<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use MyBundle\Validation\Constraint as MyAssert;


/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\generatedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @var string $email
     * @Assert\NotBlank(groups={"registration"})
     * @Assert\Email(groups={"registration"})
     * @MyAssert\Unique(groups={"registration"})
     */
    protected $email;
   .... 

堆栈追踪:

in /myApp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php at line 65    
at ErrorHandler ->handle ('2', 'class_parents() [<a href='function.class-parents'>function.class-parents</a>]: Class does not exist and could not be loaded', '/myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php', '223', array('name' => '', 'parentClasses' => array())) 
at class_parents ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 223    
at ClassMetadataFactory ->getParentClasses ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 246    
at ClassMetadataFactory ->loadMetadata ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 170    
at ClassMetadataFactory ->getMetadataFor ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/EntityManager.php at line 257    
at EntityManager ->getClassMetadata ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/EntityManager.php at line 574    
at EntityManager ->getRepository (null) 
in /myApp/src/comp/MyAppBundle/Validation/Constraint/UniqueValidator.php at line 21    
at UniqueValidator ->isValid ('asdfasdf', object(Unique)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 176    
at GraphWalker ->walkConstraint (object(Unique), 'asdfasdf', 'registration', 'data.email') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 136    
at GraphWalker ->walkMember (object(PropertyMetadata), 'asdfasdf', 'registration', 'data.email', null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 119    
at GraphWalker ->walkProperty (object(ClassMetadata), 'email', object(User), 'registration', 'data.email', null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 111    
at GraphWalker ->walkObjectForGroup (object(ClassMetadata), object(User), 'registration', 'data') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 82    
at GraphWalker ->walkObject (object(ClassMetadata), object(User), 'registration', 'data') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 162    
at GraphWalker ->walkReference (object(User), 'registration', 'data', true) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php at line 125    
at DelegatingValidator ::validateFormData (object(Form), object(ExecutionContext)) 
at call_user_func (array('Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator', 'validateFormData'), object(Form), object(ExecutionContext)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Constraints/CallbackValidator.php at line 65    
at CallbackValidator ->isValid (object(Form), object(Callback)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 176    
at GraphWalker ->walkConstraint (object(Callback), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 104    
at GraphWalker ->walkObjectForGroup (object(ClassMetadata), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 82    
at GraphWalker ->walkObject (object(ClassMetadata), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 62    
at Symfony\Component\Validator\{closure} (object(GraphWalker), 'Default') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 120    
at Validator ->validateGraph (object(Form), object(Closure), null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 65    
at Validator ->validate (object(Form)) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php at line 79    
at DelegatingValidator ->validate (object(Form)) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Form.php at line 556    
at Form ->bind (array('firstname' => '', 'lastname' => '', 'username' => '', 'email' => 'asdfasdf', 'plainPassword' => array('Passwort' => '', 'Passwort wiederholen' => ''), 'code' => '', 'plan' => '10', 'phone' => '', 'street' => '', 'zip' => '', 'city' => '', 'country' => '1')) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Form.php at line 592    
at Form ->bindRequest (object(Request)) 
in /myApp/src/comp/MyAppBundle/Controller/IndexController.php at line 151    
at IndexController ->registerAction () 
at call_user_func_array (array(object(IndexController), 'registerAction'), array()) 
in kernel.root_dir/cache/dev_local/classes.php at line 3905    
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/cache/dev_local/classes.php at line 3875    
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/cache/dev_local/classes.php at line 4879    
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 547    
at Kernel ->handle (object(Request)) 
in /myApp/web/app_dev_local.php at line 10    

1 个答案:

答案 0 :(得分:1)

at EntityManager ->getRepository (null) 
in /myApp/src/comp/MyAppBundle/Validation/Constraint/UniqueValidator.php at line 21    
at UniqueValidator ->isValid ('asdfasdf', object(Unique))

错误的原因是使用null值调用EntityManager-&gt; getRepository()。您的UniqueValidator.php似乎存在一些问题 没有那个文件,不能确切地说出问题是什么。