我有一个Symfony Form问题,我想根据预先查看的记录预先填充它。其目的是创建一种更改记录数据的方法。我通过javascript导航到表单页面,并向呈现表单的同一控制器发送ajax请求。
这是用于查看记录的页面的一部分:
<input type="button" id="changeRecord" value="change"/>
record id: <div id="recordID"> {{ record_id }} </div>
我通过javascript/jQuery访问记录id,如下所示:
var CssSelectors = new Array(
"recordID"
);
function getCurrentRecordID() {
return parseInt($.trim($("#" + CssSelectors[0]).text()));
};
javascript中的按钮代码如下:
$('#changeRecord').click(function () {
window.location.replace(Routing.generate(recordChangeRoute));
$.ajax({
url: Routing.generate(recordChangeAjaxRoute),
type: "POST",
data: {'recordID': getCurrentRecordID()}
});
// both Routes point to the same controller
// problem located here ???
Symfony控制器操作如下:
public function changePlasmidRecordAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$recordHandle = $em->getRepository('DataBundle:RecordClass');
$AjaxRequest = Request::createFromGlobals();
$RecordID = $AjaxRequest->request->get('recordID');
$recordToUpdate = $recordHandle->findOneBy(array('id' => $RecordID));
$updateForm = $this->createForm(new RecordClassType(), $recordToUpdate);
$updateForm->handleRequest($request);
if ($updateForm->isValid()) {
$em->flush();
return this->redirect($this->generateUrl('route_showRecord'));
} else {
return $this->render('DataBundle:RecordClass:updateRecord.html. twig',
array(
'RecordID' => $RecordID,
'form' => $updateForm->createView()
));
}
}
我正在努力实现的是:
- 查看记录
- 转到预填充表单
- 进行更改并保存
查看记录、创建表单、将更改持久化到数据库——所有这些都可以完成。不起作用的是访问控制器内部所需的ID。我可以像在其他控制器操作中尝试的那样访问ajax请求数据,而不会出现问题。"表格申请"是如何干扰的?还是这样?我必须使用事件侦听器吗?
非常感谢任何帮助,提前感谢!
我通过保留ajax内容并通过GET提交所需数据来修复它。像这样:
$('#changeRecord').click(function () {
window.location.replace(Routing.generate(recordChangeRoute, {'recordID': getCurrentRecordID()}));
});
谢谢你的意见,祝你一切顺利。