2012-01-13 9 views
1

のために、私は次の検証制約をRegistrationFormType持っている:ここで説明するように、私は(ConstraintValidatorを拡張する)一意のクラス(Contraintの延長)とUniqueValidatorを作成した一意性を確保するために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, 
     ); 
    } 

を: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 

それは思えます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

答えて

1

見つかり問題:

私がUNIQUE制約でコードを次していた:

/** 
* @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'; 
    } 
} 

そして、これが1を補正しています。

/** 
* @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

関連する問題