I am trying to build symfony2 rest api, but I am struggling with FOSRestBundle
At first I could make work "@View()" annotation because it always would say that I template is missing, not matter what config options I would use I would get the same error, then in my routing yml I added "defaults: { _format: json }" and suddenly it stated working.
v1:
resource: "@AppBundle/Controller/"
defaults: { _format: json }
type: annotation
prefix: /v1
But now I am having problem with forms I create a form class and a service and setup simple controller method:
/**
* 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;
}
Now if form is invalid which is in my test case, I would expect to get similar response to that, that is showed in documentation: http://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views
but instead of something like this:
{
"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."
]
}
}
}
}
I get:
{
"children": {
"code": [],
"name": [],
"description": []
}
}
What's happening here? Is something wrong with my setup or I am missing something? By the way, I set form validation in my entity
{
"children": {
"code": [],
"name": [],
"description": []
}
}
This mean that form is valid and there weren't any errors in validation. So, it's correct response because You said Your test case has got valid form.
Fragment of response from below will show up if field will be defined as "required" in form declaration and with validation constraint NotBlank ( http://symfony.com/doc/current/reference/constraints/NotBlank.html ).
"errors": [
"This value should not be blank."
]
EDIT:
After discussion in comments: replacing "handleRequest" with "submit" helped.