$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
if ($request->isXmlHttpRequest()) {
return new JsonResponse([
'id' => $entity->getId(),
]);
}
return $this->redirectToRoute('route');
}