I am building an REST API with Symfony 2.7.2, using Doctrine, FOSRestBundle and JMSSerializerBundle. I was creating controller for my Address class using this pattern.
My Problem is that using default id->entity ParamConverter and passing converted Entity to a form type for updating it causes tha validation to fail as shown below. BUT if i replace it with an old-school $id and I fetch the entity myself from repository, it works fine.
Is it bug or have I missed something?
Relevant code follows.
So I have a controller method for get (HTTP GET) entity by id:
/**
* @Route("addresses/{id}", requirements={"id" = "\d+"}, name="base_address_entity")
* @Method({"GET"})
*/
public function detailAction(Address $address)
{
return $address;
}
(works fine)
Another action for create new entity (HTTP POST)
/**
* @Route("addresses")
* @Method({"POST"})
*/
public function insertAction(Request $request)
{
return $this->processForm(new Address(), $request);
}
(also works fine)
..and an update action (HTTP PUT), using default id ParamConverter as in the DetailAction:
/**
* @Route("addresses/{id}", requirements={"id" = "\d+"}, name="base_address_create")
* @Method({"PUT"})
*/
public function updateAction(Address $address, Request $request)
{
return $this->processForm($address, $request);
}
and this one fails with an unspecified form error:
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": [
"This value is not valid."
],
"children": {
...no errors in any of the of the other fields
The process form function is the same for both insertAction and updateAction:
public function processForm(Address $address, Request $request)
{
/** @var AddressRepository $addressRepository */
$form = $this->createForm($this->get('address.form_type'), $address);
$form->submit($request->get($form->getName()));
if ($form->isValid())
{
$addressRepository = $this->get('address.repository');
$addressRepository->save($address);
return $this->get('success');
}
return $form;
}