2011-12-08 28 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)存儲用方括號 - 我想明白爲什麼順豐做到這一點。我必須手動刪除所有似乎荒謬的括號,所以我認爲我錯過了某處的某些功能。

$錯誤的轉儲:

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

屬性路徑可以映射到屬性或索引。考慮實施\ArrayAccessOptionBag類和getSize()的方法。

  • 屬性路徑size$optionBag->getSize()
  • 屬性路徑[size]$optionBag['size']

在你的情況,你驗證的數組。由於數組元素也可以通過索引訪問,因此違例中的結果屬性路徑包含方括號。

更新:

您不必手動刪除的方括號。您可以使用的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

它真的很吸引人,不像laravel驗證程序,現在我使用了很多laavel後,我必須使用symfony,那很無聊,我不明白誰創建了symfony,可能不適合人類......也許對於受虐狂者 – errogaht

相關問題