我想建立Symfony2的REST API,但我與FOSRestBundleFOSRestBundle形成
掙扎起初,我可以做的工作「@View()」的註釋,因爲它總是會說,我的模板丟失了,沒有關係我會使用什麼配置選項我會得到相同的錯誤,然後在我的路由yml中添加「默認值:{_format:json}」,並突然聲明工作。
v1:
resource: "@AppBundle/Controller/"
defaults: { _format: json }
type: annotation
prefix: /v1
但隨着形式,現在我有問題,我創建了一個窗體類和服務,並建立簡單的控制器方法:
/**
* Creates a new Order entity.
*
* @Rest\Post("/products", name="products_create")
* @Rest\View()
* @param Request $request
* @return Product|array
*/
public function createAction(Request $request)
{
$product = new Product();
$form = $this->createForm('product', $product);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $product;
}
return $form;
}
如果現在的形式是這是在我的測試情況下無效的,我會期望獲得到類似的反應,即顯示在文檔:http://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views
但不是這樣的:
{
"code": 400,
"message": "Validation Failed";
"errors": {
"children": {
"code": {
"errors": [
"This value should not be blank."
]
},
"name": {
"errors": [
"This value should not be blank."
]
},
"description": {
"errors": [
"This value should not be blank."
]
}
}
}
}
我得到:
{
"children": {
"code": [],
"name": [],
"description": []
}
}
這裏發生了什麼?我的設置有問題或我錯過了什麼?順便說一下,我在我的實體中設置了表單驗證
表單爲「無效」無效 –
對不起,我的錯誤。您確定在實體中您將這些字段的驗證定義爲「NotBlank」嗎? – KLXN
是的,我也嘗試直接在表單類中定義表單驗證,得到了相同的結果 –