我正在使用Symfony,版本2.5.3,我正在尝试使用Ajax提交表单。 所以,在我的控制器中我有
$form = $this->createForm(
new MyFormType(),
$formvariable
);
$form->handleRequest( $request );
if ($form->isValid()) {
// do stuff
} else {
// other stuff
}
我的jQuery脚本可能与此类似:
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
},
});
这样可以正常工作,但遗憾的是我必须将其他变量与表单的字段一起发送。
我想在jQuery中做的是:
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
},
});
但是,这样控制器内的$form->isValid()
总是返回false。
有没有办法解决这个问题? 提前谢谢
阿尔贝托
修改 添加一些其他详细信息,以回应评论:
我无法在表单中添加一些额外的字段,因为其他变量用于生成表单本身。
控制器的更多细节:
$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
答案 0 :(得分:3)
控制器应该看起来像我建议的那样:
/**
* @Route("/myentity/{myentid}")
*/
public function newAndEditAction(Request $request, $myentid = null)
{
$em = $this->getDoctrine()->getManager();
// Here I determine if I have to create a form for a new entity or an
// entity already present in the database
if ($myentid)
{
// Retrieve entity from database
$formvariable = $this->getDoctrine()->getManager()
->getRepository('MyBundle:MyEntity')
->find($myentid);
} else {
// New entity
$formvariable = new MyEntity();
}
$form = $this->createForm(
new MyFormType(),
$formvariable
);
if ($request->getMethod() == 'POST') { // save
$form->handleRequest($request);
if ($form->isValid()) {
$em->flush();
// return some response
return Response(...);
}
} else { // edit and new
return new Response($this->render('MyBundle:Controller:mytwigview.html.twig', array(
'form' => $form->createView(),
))->getContent());
}
}
我为此操作设置了id参数可选,并根据id参数和请求方法决定是保存/更新并显示新表单还是编辑表单。
答案 1 :(得分:0)
我使用以下内容通过Ajax一次提交两个表单。这也是你问题的答案:
$.ajax({
type: "POST",
url: Routing.generate('ajax_report_processing_create_pdf'),
data: $reportMainWrapper.find('form[name="report"]').serialize() + '&' +
$reportProcessingForm.find('form').serialize(),
dataType: "json"
})
.done(function (data) {
console.log('done');
console.log(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log('fail');
console.log(jqXHR);
})
.always(function () {
console.log('always');
});
如您所见,您可以使用:
data: 'myvar1=' + data1 + '&' + 'myvar2=' + data2 + '&' + formdata,
如你所见,我没有'formdata=' + formdata,
。这是因为您的变量formdata
已包含键值数据:
form[yourinput1] = 'string1'
form[yourinput2] = 'string2'
因此,在您的控制器中,您现在只需执行$form->handleRequest($request);
即可立即生效。