[英]Guzzle annotations not working with Doctrine2 annotation parser in Symfony2 project
[英]Symfony2 Validation using Doctrine2 Annotations
我有以下Doctrine實體,我也想使用其限制進行驗證。
/**
* @var string
*
* @ORM\Column(type="string", length=40)
* @Assert\Valid
*/
private $birthName;
我使用以下驗證,它適用於針對symfony的特定注釋, 但不適用於Doctrine設置的限制!
// Validate data
$validator = $this->get('validator');
$errors = $validator->validate($user);
if (count($errors) > 0) {
$response = new JsonResponse(array(
'error' => 'User could not be created.' . PHP_EOL . (string)$errors
));
$response->setStatusCode(400);
return $response;
}
我該怎么做才能讓symfony驗證程序將學說限制用作設置?
現狀:
我讀了[ 1 ]和[ 2 ],但到目前為止,我沒有使用表單,因為我有一個返回JSON的控制器。 如果您知道如何使用表格來完成這項工作,也將大有幫助!
原則映射與驗證無關。 代碼@ORM\\Column(type="string", length=40)
僅將屬性映射到數據庫字段,並且如果您要使用原則創建架構,則將數據庫字段的最大長度設置為等於40個字符。
但是因此不參與驗證過程的任何部分。 因此,您需要通過以下方式設置斷言規則
/**
* @Assert\Length(max = 40)
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.