4

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

antanas_sepikas
  • 5,644
  • 4
  • 36
  • 67

2 Answers2

3
{
"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.

KLXN
  • 660
  • 5
  • 13
  • the form is "invalid" not valid – antanas_sepikas Apr 26 '15 at 17:36
  • sorry, my mistake. Are You sure that in entity You defined validation for these fields as "NotBlank"? – KLXN Apr 26 '15 at 17:51
  • Yes, I also tried defining form validation in form class directly, got same results – antanas_sepikas Apr 26 '15 at 18:04
  • And also You've cleared cache after adding validation to entity? :) – KLXN Apr 26 '15 at 18:08
  • Yes I did, this is strange issue, I don't know what's happening. – antanas_sepikas Apr 26 '15 at 18:10
  • You can try also other approach - getting errors by $form->getErrors(). Look at Cedo's answer here: http://stackoverflow.com/questions/6978723/symfony2-how-to-get-form-validation-errors-after-binding-the-request-to-the-fo – KLXN Apr 26 '15 at 18:13
  • Yes I did, this is strange issue, I don't know what's happening. The form get's invalidated, but no errors, are displayed, also If I dump my entity after handleRequest I see no parameters mapped. About the getErrors, I tried, I shows that errors array empty. – antanas_sepikas Apr 26 '15 at 18:16
  • 2
    It's strange indeed. I don't have any other idea. Maybe You can experiment with trying to pass to createForm form object as first parameter (not string)? Or replacing handleRequest with submit? – KLXN Apr 26 '15 at 18:31
  • Replacing handleRequest with submit did helped. Though I have no idea why? Anyway I get the result I need, if you would modify your answer I would definitely wold accept it. – antanas_sepikas Apr 26 '15 at 18:51
  • To be honest, i don't know why either, but i'm glad it helped. :) I edited my answer. – KLXN Apr 26 '15 at 18:55
  • handleRequest won't work in this case as there is no form submission in your request. You have to manually get the data from the request and manually submit it. – mark.sagikazar Mar 08 '16 at 20:06
1

I know this is marked as solved, but I recently encountered the same issue with FOSRestBundle not returning an invalid form in the way it is documented at

http://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views

I found the actual issue has to do with the order of registering bundles in AppKernel when JMS Serializer is used. You MUST make sure to load in this order:

new JMS\SerializerBundle\JMSSerializerBundle(),
new FOS\RestBundle\FOSRestBundle()

Once I did that, I started seeing results consistent with the documentation when a form fails validation.