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?