2012-01-13 70 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