简体   繁体   English

Symfony2将Ajax请求数据传递到表单呈现控制器

[英]Symfony2 Passing ajax request data to a form rendering controller

I have a problem with a Symfony Form which I want to prefill based on which record is viewed beforehand. 我有一个Symfony表单有问题,我想根据事先查看的记录来预填该表单。 The intention is to create a means for changing the record data. 目的是创建一种用于更改记录数据的方法。 I navigate to the form page via javascript and send an ajax request to the same controller the form is rendered by. 我通过javascript导航到表单页面,并将ajax请求发送到呈现表单的同一控制器。

This is part of the page used for viewing records: 这是用于查看记录的页面的一部分:

    <input type="button" id="changeRecord" value="change"/>

    record id: <div id="recordID"> {{ record_id }} </div>

I access the record id through javascript/jQuery like this: 我像这样通过javascript / jQuery访问记录ID:

    var CssSelectors = new Array(
        "recordID"
    );

    function getCurrentRecordID() {
        return parseInt($.trim($("#" + CssSelectors[0]).text()));
    };

The button-code in javascript is the following: 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 ???

The Symfony Controller Action is the following: 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()
        ));
    }
}

What I am trying to achieve is: 我想要实现的是:

  • view a record 查看记录
  • go to prefilled form 转到预先填写的表格
  • make changes and save 进行更改并保存

Viewing records, creating the form, persisting the changes to the database - all work. 查看记录,创建表单,将更改持久保存到数据库-所有工作。 What does not work is accessing the needed ID inside the controller. 不起作用的是访问控制器内部所需的ID。 I can access the ajax request data the way I try in an other controller action without problems. 我可以按照在其他控制器操作中尝试的方式访问ajax请求数据,而不会出现问题。 How is the "Form Request" interfering? “表单请求”如何干预? Or is it? 还是? Do I have to use an Event Listener? 我必须使用事件监听器吗?

Any help is greatly appreciated, thanks in advance! 非常感谢您的任何帮助,在此先感谢您!

I fixed it by leaving the ajax stuff away and submitting the required data via GET. 我通过保留ajax内容并通过GET提交所需的数据来修复它。 Like so: 像这样:

    $('#changeRecord').click(function () {
    window.location.replace(Routing.generate(recordChangeRoute, {'recordID': getCurrentRecordID()}));
    });

Thank you for your input, all the best. 谢谢您的宝贵意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM