简体   繁体   English

symfony2:使用ajax持久化实体

[英]symfony2 : persist entity with ajax

In a page, i have some element which users can click to edit. 在页面中,我有一些元素,用户可以单击以进行编辑。 I want to add a function to edit the date with a simple drag ot the event in a calendar grid. 我想添加一个功能,通过简单地拖动日历网格中的事件来编辑日期。 The user could quickly edit the date by dragging the event down, or click on the event to edit more things. 用户可以通过向下拖动事件来快速编辑日期,或者单击事件以编辑更多内容。

For now, I want to persist my entity with an ajax call when the user click on an event. 现在,我想在用户单击事件时通过ajax调用保留我的实体。 I can retrieve my data ( event' id and the start_date )I tried to put them in a simple .txt file and it work. 我可以检索我的数据( event' idstart_date ),我试图将它们放在一个简单的.txt文件中,并且可以正常工作。

My ajax : 我的ajax:

var start_date = event.start_date;
var id = id;
var edit = Routing.generate('admin_event_edit', {id : id});

$.ajax({
    url: Routing.generate('admin_event_edit', {id : id}),
    type: "POST",
    data: { id : id, start_date : start_date},
    success: function(data) {
        alert('ok');
    },
    error: function(){
        alert('error');
    }
});

i have a form at the route admin_event_edit And in my controller : 我在admin_event_edit路线上有一个表格,并且在我的控制器中:

public function editAction(Request $request, $id)
{
     $em = $this->getDoctrine()->getManager();
     $entity = $em->getRepository('AppBundle:Event')->find($id);
     $starDate = $request->request->get('start_date');

    $editForm = $this->createForm(new EventType(), $entity);

    $editForm->handleRequest($request);

    if ($editForm->isValid())
    {
        if($request->isXmlHttpRequest())
        {

            $entity->setStartDate($starDate);
            $entity->setNotice('azeazedfdf');

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
        }

    }

        return $this->redirect($this->generateUrl('admin_index'));

}

I have the ajax alert ok and i can see the ajax call has been sent in the symfony profiler, but my entity isn't updated. 我的ajax警报ok ,可以在symfony探查器中看到已经发送了ajax调用,但是我的实体没有更新。 What's wrong with my code ? 我的代码有什么问题?

Thanks 谢谢

The $form isn't valid. $ form无效。

The Symfony Form needs the CSRF Token to be valid, it's an hidden field within the generated form. Symfony表单需要CSRF令牌有效,这是生成的表单中的隐藏字段。

Since you're submitting this form with AJAX instead of the normal way, the isValid() check will always fail, because you don't send all necessary data in your AJAX request. 由于您是使用AJAX而非常规方式提交此表单的,因此isValid()检查将始终失败,因为您不会在AJAX请求中发送所有必要的数据。

Example: 例:

<input id="form__token" class="form-control" type="hidden" value="Poo4GKPb2pcchnRXAJd74ZxLqTU2xMrgHKi8QHN4N8Y" name="form[_token]">

You need to send this value with your AJAX call, if no more information is missing your code should work as far as I can tell. 您需要通过AJAX调用发送此值,如果没有更多的信息丢失,那么您的代码应该可以正常运行。

I made it work. 我成功了 In fact i didn't needed to go through my form : 实际上,我不需要填写表格:

public function editAction(Request $request, $id)
{
     $em = $this->getDoctrine()->getManager();
     $entity = $em->getRepository('AppBundle:Event')->find($id);
     $startDate = $request->request->get('start_date');
     $endDate = $request->request->get('end_date');

    if ($request->isMethod('POST'))
     {

        if($request->isXmlHttpRequest())
        {
            $entity->setStartDate(new \DateTime($startDate));
            $entity->setEndDate(new \DateTime($endDate));
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

        }

    }

    return //your stuff, me i don't need to be redirected
}

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

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