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:
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, ); }
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:
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, ); }
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
post put symfony request fosrestbundle
rollsappletree
source share