2012-01-13 115 views
1

我有下面的验证约束的RegistrationFormType:Symfony2中:UniqueValidator为形式

public function getDefaultOptions(array $options) 
    { 
     $collectionConstraint = new Collection(array(
      'email' => array(
       new NotBlank(), 
       new Email(array('message' => 'Ungültige E-Mail Adresse')), 
       ), 
      'username' => new Unique(), 
      'code' => new MaxLength(array('limit'=>20)), 
      'plainPassword' => new MaxLength(array('limit'=>20)), 
     )); 

     return array(
      'csrf_protection' => false, 
      'validation_constraint' => $collectionConstraint, 
     ); 
    } 

为了保证唯一性我创建一个唯一的类(延伸Contraint)和UniqueValidator(延伸ConstraintValidator)像这里所描述:http://www.michelsalib.com/2011/04/create-your-own-constraint-validator-in-symfony2-a-doctrine-unique-validator/

的问题是,我得到提交表单以下错误:

Catchable Fatal Error: Argument 1 passed to ...\Validation\Constraint\UniqueValidator::__construct() must be an instance of Doctrine\ORM\EntityManager, none given 

这似乎s表示了EntityManager没有注入ConstraintValidator,我在config.yml创建服务定义,但:

services: 
    eventiply.validator.unique: 
     class: Ajado\EventHubBundle\Validation\UniqueValidator 
     arguments: 
      entityManager: "@doctrine.orm.entity_manager" 
     tags: 
      - { name: validator.constraint_validator, alias: validator.unique } 

任何想法如何,我可以在这里进步?

+0

我有一个类似的[问题](http://stackoverflow.com/questions/8403440)。我从来没有解决它。 – gilden 2012-01-13 20:58:37

回答

1

发现了问题:

我曾在我的唯一约束下面的代码:

/** 
* @Annotation 
*/ 
class Unique extends Constraint{ 
    public $message = 'Dieser Wert muss eindeutig sein, ist leider aber schon vergeben'; 
    public $propertyName; 
    public $property; 
    public $entityName; 

    public function __construct($options = null) { 

     parent::__construct($options); 
    } 

    public function validatedBy() 
    { 
     // copied that from http://symfony.com/doc/current/cookbook/validation/custom_constraint.html 
     return get_class($this).'Validator'; 
    } 
} 

,这是纠正一个:

/** 
* @Annotation 
*/ 
class Unique extends Constraint{ 
    public $message = 'Dieser Wert muss eindeutig sein, ist leider aber schon vergeben'; 

    public function __construct($options = null) { 

     parent::__construct($options); 
    } 

    public function validatedBy() 
    { 
     return 'validator.unique'; 
    } 

}

0

你的YAML看起来不对。该arguments值应该是一个数组,而不是一个哈希:

arguments: 
    - "@doctrine.orm.entity_manager" 
+0

改变了它 - 没有任何影响 – stoefln 2012-01-18 10:22:48