2011-12-08 9 views
3

私はいくつかの値を検証しています:なぜsymfony2の検証propertyPath valusが角括弧で囲まれていますか?

$collectionConstraint = new Collection(array(
    'email' => array(
     new NotBlank(), 
     new Email(), 
    ), 
    'password' => array(
     new NotBlank(), 
     new MinLength(array('limit' => 6)), 
     new MaxLength(array('limit' => 25)), 
    ), 
)); 
$data = array('email' => $this->getRequest()->get('email'), 'password' => $this->getRequest()->get('password')); 
$errors = $this->get('validator')->validateValue($data, $collectionConstraint); 

をしかし、いくつかの理由でフィールド(のPropertyPath)は角括弧で保存されている - 私は、Sfはそれを行う理由を理解したいと思います。私は手作業ですべてのブラケットを取り除かなければならないと思われるので、私はどこかの機能が不足していると思う。 $エラーの

ダンプ:

Symfony\Component\Validator\ConstraintViolationList Object 
(
    [violations:protected] => Array 
     (
      [0] => Symfony\Component\Validator\ConstraintViolation Object 
       (
        [messageTemplate:protected] => This value should not be blank 
        [messageParameters:protected] => Array 
         (
         ) 

        [root:protected] => Array 
         (
          [email] => 
          [password] => 
         ) 

        [propertyPath:protected] => [email] 
        [invalidValue:protected] => 
       ) 

      [1] => Symfony\Component\Validator\ConstraintViolation Object 
       (
        [messageTemplate:protected] => This value should not be blank 
        [messageParameters:protected] => Array 
         (
         ) 

        [root:protected] => Array 
         (
          [email] => 
          [password] => 
         ) 

        [propertyPath:protected] => [password] 
        [invalidValue:protected] => 
       ) 

     ) 

) 

でもToString関数は無用です。

"[email]: This value should not be blank","[password]: This value should not be blank" 

答えて

5

プロパティパスは、プロパティまたはインデックスのいずれかにマップできます。 \ArrayAccessとメソッドgetSize()を実装するクラスOptionBagを考えてみましょう。

  • プロパティパスsizeは、プロパティパス[size]は、あなたのケースでは$optionBag['size']

を指し、あなたは、配列を検証$optionBag->getSize()

  • を指します。配列要素もインデックスでアクセスされるため、違反の結果として生じるプロパティパスに角括弧が含まれます。

    更新:

    手動二乗括弧を削除する必要はありません。次の例のように、あなたのデータと同じ構造を持つ配列にエラーをマップするためにsymfonyのPropertyAccessコンポーネントを使用することができます。

    $collectionConstraint = new Collection(array(
        'email' => array(
         new NotBlank(), 
         new Email(), 
        ), 
        'password' => array(
         new NotBlank(), 
         new MinLength(array('limit' => 6)), 
         new MaxLength(array('limit' => 25)), 
        ), 
    )); 
    
    $data = array(
        'email' => $this->getRequest()->get('email'), 
        'password' => $this->getRequest()->get('password') 
    ); 
    
    $violations = $this->get('validator')->validateValue($data, $collectionConstraint); 
    
    $errors = array(); 
    $accessor = $this->get('property_accessor'); 
    
    foreach ($violations as $violation) { 
        $accessor->setValue($errors, $violation->getPropertyPath(), $violation->getMessage()); 
    } 
    
    => array(
        'email' => 'This value should not be blank.', 
        'password' => 'This value should have 6 characters or more.', 
    ) 
    

    これはまた、多次元データ配列で動作します。プロパティパスは[author][name]のようになります。 PropertyAccessorは、$errors配列の同じ場所にエラーメッセージを挿入します($errors['author']['name'] = 'Message')。

  • +0

    どのようにすればこのアプローチを利用できますか?私はネストされた配列の検証が[]内のインデックスで起こる可能性があることを理解できましたが、配列からの単一のインデックスのときは[]を持たないべきだと思います。バリデーションを行う人は、バリデーション(オブジェクトまたは配列)を認識している必要があります。 – aambrozkiewicz

    +0

    確定的なエラーマッピングを実行する場合は、そのことを知る必要があります。上記のgetSize()と['size'](仮説)の入力を表示するフォームを考えてみましょう。エラーをマッピングするには、各違反がどのフィールドに属するかを正確に知る必要があります。 –

    +0

    これは本当にラヴェルバリデーターとは違っています。ラヴェルをたくさん使った後、私はsymfonyを使用しなければなりません。とても退屈です。私はsymfonyが誰のために作られたのか理解できません。 – errogaht

    関連する問題