FosRestBundle post / put [create new / updating object] does not read the request correctly

long story short: Using FOSRestBundle I'm trying to create some objects through a POST call or modify existing ones through PUT.

here is 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 json as {"description": "Japan"}, it changes my country with id = 1, omitting the empty description.

If instead I try to create a NEW object using 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 

he gives me an error:

 exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null 

It seems that I cannot correctly submit the form binding request.

Please note that if I json_decode the request suggested here , it responds

 { "code":400, "message":"Validation Failed", "errors":{ "errors":[ "This value is not valid." ], "children":{ "description":[ ] } } } 

Any advice?

Thanks, Rolls

+8
post put symfony request fosrestbundle
source share
2 answers

I decided:)

this is why it has not worked before:

In my form definition, the name was "zanzibar_backendbundle_countries".

 public function getName() { return 'zanzibar_backendbundle_countries'; } 

So, to associate a request with this form, json should look 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 place json with a placeholder, like

 "something":{"key":"value"} 

your form name must be exactly "something"

+21
source share

Try the following:

 if ($form->isValid()) { $entity = $form->getData(); // <------ you didn't bind entity to the form $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); 
0
source share

All Articles