I am using Symfony, version 2.5.3, and I am trying to submit a form with Ajax. So, in my controller I have
$form = $this->createForm(
new MyFormType(),
$formvariable
);
$form->handleRequest( $request );
if ($form->isValid()) {
// do stuff
} else {
// other stuff
}
and my jQuery script could be similar to this:
formdata = $('form[name=MyFormTypeForm]').serialize();
$.ajax({
type: "POST",
url: // my url,
data: formdata ,
async: true,
dataType: "json",
success: function(response)
{
// stuff here
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
// stuff there
},
});
This would work fine, but unfortunately I have to send other variables together with the fields of the form.
What I want to do in my jQuery is something like:
formdata = $('form[name=MyFormTypeForm]').serialize();
value = {
myvar1 : data1 ,
myvar2 : data2 ,
formdata : formdata ,
};
$.ajax({
type: "POST",
url: // my url,
data: value ,
async: true,
dataType: "json",
success: function(response)
{
// stuff here
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
// stuff there
},
});
However, in this way $form->isValid() inside the controller always returns false.
Is there a way to fix this?
Thank you in advance
Alberto
Edit: Adding some additional details, in response to comments:
I cannot add some extra fields inside my form because the additional variables are used to generate the form itself.
More details of the controller:
$em = $this->getDoctrine()->getManager();
$action = $request->request->get('action', null);
$myentid = $request->request->get('myentid', null);
// Here I determine if I have to create a form for a new entity or an
// entity already present in the database
if ( empty($myentid) )
{
// New entity
$formvariable = new MyEntity();
} else {
// Retrieve entity from database
$formvariable = $this->getDoctrine()->getManager()
->getRepository('MyBundle:MyEntity')
->find($myentid);
}
$form = $this->createForm(
new MyFormType(),
$formvariable
);
// I am requesting a new form: render the form and send to page
if ($action == "newform")
{
$response["code"] = $this->render('MyBundle:Controller:mytwigview.html.twig', array(
'form' => $form->createView(),
))->getContent();
return new Response(json_encode($response));
}
// The form has already been filled: save
if ($action == "save")
{
$form->handleRequest( $request);
if ($form->isValid()) {
$em->flush();
} else {
echo "Errors: ".$form->getErrors();
}
}
// And here I return the save confirmation