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"
我如何從這種方法中受益?我可以理解,驗證嵌套數組可能會導致索引在[]中,但是當它是一個數組中的單個索引時,它認爲它不應該有[]。驗證的人應該知道他正在驗證(對象或數組)。 – aambrozkiewicz
如果你想做確定性錯誤映射,你需要知道這一點。考慮一個表單,它顯示上面getSize()的一個輸入,另一個表示['size'](假設)。爲了映射錯誤,您需要準確知道每個違規所屬的字段。 –
它真的很吸引人,不像laravel驗證程序,現在我使用了很多laavel後,我必須使用symfony,那很無聊,我不明白誰創建了symfony,可能不適合人類......也許對於受虐狂者 – errogaht