long story short: Using FOSRestBundle I'm trying to create some entities via POST call, or modify existing via PUT.
here the code:
/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putCountriesAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
    $form->bind($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }
    return array(
        'form' => $form,
    );
} //[PUT] /countries/{id}
If I call /countries/{id} with PUT passing a json like {"description":"Japan"}, it modify my country with id=1, putting an empty description.
If, instead, I try to create a NEW entity with this method:
/**
 * Create new Countries (in batch)
 * @param  Request $request json request
 * @return array           redirect to get_coutry, will show the newly created entities
 */
public function postCountriesAction(Request $request)
{
    $entity = new Countries();
    $form = $this->createForm(new CountriesType(), $entity);
    $form->bind($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->redirectView(
            $this->generateUrl(
                'get_country',
                array('id' => $entity->getId())
            ),
            Codes::HTTP_CREATED
        );
    }
    return array(
        'form' => $form,
    );
} //[PUT {"description":"a_description"}] /countries
it gives me an error saying:
exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null
so seems that i'm not able to pass correctly the request to bind to the form.
Notice that if i json_decode the request as suggested here it reply with a
{
    "code":400,
    "message":"Validation Failed",
    "errors":{
        "errors":[
            "This value is not valid."
        ],
        "children":{
            "description":[
            ]
        }
    }
}
Any Advice?
Thanks, Rolls
I solved :)
this is the reason why it did'nt worked before:
In my Form definition the name was "zanzibar_backendbundle_countries".
public function getName()
{
    return 'zanzibar_backendbundle_countries';
}
So to bind a request to this form the json should have looked like this:
{"zanzibar_backendbundle_countries": [{"description": "Japan"}]}
Since I wanted it to be something like
{"id":1,"description":"Italy"}
I had to remove the name from the Form:
public function getName()
{
    return '';
}
In general if you want to post a json with a placeholder like
"something":{"key":"value"}
your form name must be exactly "something"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With