0

I'm trying to figure out why the form is not validating when I create one with a pre-populated entity with values coming from a json request.

Here is the controller in Symfony with the FosRestBundle already configured:

public function createAction(Request $request)
{
    $house = new House();
    $house->setTitle($request->get('title'));
    $house->setDescription($request->get('description'));
    $house->setPostcode($request->get('postCode'));
    $house->setPhoneNumber((int) $request->get('phoneNumber'));
    $availability = $request->get('available') ? true : false;
    $house->setAvailability($availability);

    $form = $this->createCreateForm($house);

    $form->handleRequest($request);

    $response = new JsonResponse();

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($house);
        $em->flush();

        return $response->setData(array(
            'success' => true
        ));
    }

    return $response->setData(array(
        'success' => false,
        'errors' => $this->getFormErrors($form)
    ));
}

private function createCreateForm(House $entity)
{
    $form = $this->createForm(new HouseType(), $entity, array(
        'action' => $this->generateUrl('houses_create'),
        'method' => 'POST',
        'csrf_protection' => false
    ));

    return $form;
}

the yaml config file:

# app/config/config.yml
fos_rest:
    param_fetcher_listener: true
    body_listener: true
    routing_loader:
        default_format: json
    exception:
        enabled: true
    # configure the view handler
    view:
        force_redirects:
            html: true
        formats:
            json: true
            xml: true
        templating_formats:
            html: true
    # add a content negotiation rule, enabling support for json/xml for the entire website
    format_listener:
        enabled: true
        rules:
            - { path: ^/, priorities: [ json, xml, html ], fallback_format: html, prefer_extension: false }

If I execute $form->get('title')->getData() for example I can see that the form is filled correctly but still doesn't pass the validation and when I execute $this->getFormErrors($form) I just get an empty array.

Any idea on how I could debug this issue?

2 Answers 2

1

In order to receive json, you need to enable the body listener feature.

In your config.yml:

fos_rest:
    body_listener: true

You should also check the documentation for advanced usage.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, I've updated the yml config file consequently but still having the same issue.
Does the request that you are sending contain the header "Content-Type: application/json"?
As said to sw0rdfishhhhh I just edited the issue accordingly to the latest debug, thanks
0

I noticed a typo when you initiate the $form: "createCreateForm" -> it should be just "createForm".

If that doesn't work check the values from $form->getData() after the "if ($form->isValid()) { " part. I suspect the framework doesn't know how to handle the request (not coming from a standard form). It that's the case then you can set manually the values on the entity and save it.

$house->setStuff($request->request->get('stuff');
...
$em->persist($house);`$em->flush();`

4 Comments

How about the contents of $form->getData()? Just var_dump it.If that is empty or not helpful, try going step by step with a debugger (if you have xdebug set up). Most likely the values you need are in the $request variable.
Just did some debugging on this one today. It seems like the values are there in the form but still $form->isValid() returns false and when I execute $this->getFormErrors($form) I get an empty array.
Just edited the title and description of the issue raised accordingly. Thanks so far.
Have a look at this: stackoverflow.com/questions/11208992/… . Double check your constraints on the entity and the values that are set on the entity before calling isValid() .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.