dongmeirang4679 2016-08-08 12:52
浏览 17

Symfony 2 HandleRequest似乎无法正常工作

I'm trying to submit a form using AJAX with POST method.

I'm using symfony 2.8.9

The main page is a Fullcalendar view and the form is inside a popUp modal :

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Définition d'un événement</h4>
        </div>
        {{ form_start(FORM) }}
        <div class="modal-body">
            {{ form_row(FORM.dateDebut) }}
            {{ form_row(FORM.dateFin) }}
            {{ form_row(FORM.salle, {'attr' : {'onChange' : 'afterSelectSalle();'} }) }}
            {{ form_row(FORM.formateur, {'attr' : {'onChange' : 'afterSelectFormateur();'} }) }}
            {{ form_row(FORM.module, {'attr' : {'onChange' : 'afterSelectModule();'} }) }}
            {{ form_row(FORM.promotion, {'attr' : {'onChange' : 'afterSelectPromotion();'} }) }}
            {{ form_row(FORM.type) }}
        </div>
        <div class="modal-footer">
            <input type="hidden" id="{{ FORM.vars.name }}_eventId" value=""/>
            <button type="button" id="enregistrer" class="btn btn-success" data-dismiss="modal">Enregistrer</button>
            <button type="button" id="annuler" class="btn btn-default" data-dismiss="modal">Annuler</button>
            <button type="button" id="supprimer" class="btn btn-danger" data-dismiss="modal">Supprimer</button>
        </div>
        {{ form_end(FORM) }}
        <div class="modalAttente"></div>
    </div>
</div>

This is the method i'm calling to save the data which are set in the previous form :

function saveInBase(donnees) {
    var ajout = true;
    if(eventSelectionne !== null) {
        ajout = false;
    }
    $.ajax({
        type: 'POST',
        url: ajout ? Routing.generate('planning_create') : Routing.generate('planning_edit', {id: donnees.id}),
        dataType: 'json',
        data: donnees,
        success: function (data) {
            eventSelectionne = getInfosEvent(eventSelectionne, data);
            if (ajout) {
                $('#calendar').fullCalendar('renderEvent', eventSelectionne,     true);
            } else {
                $('#calendar').fullCalendar('updateEvent', eventSelectionne);
            }
        },
        complete: function () {
            $('.btn#enregistrer').prop('disabled', false);
            displayModalAttente(false);
        }
    });
}

The problem I face is that my controller return a 500 error when I'm submitting the form.

public function createAction(Request $request)
{
    $event = new Event();
    $form = $this->createForm(
            EventType::class,
            $event
        );
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        $event->setDateSaisie(new \DateTime);
        $em->persist($event);
        $em->flush();
        return new JsonResponse($event->toJson(), JsonResponse::HTTP_OK, array('Content-Type' => 'application/json'));
    }
    return new JsonResponse(array('errors' => $this->getErrorsAsArray($form)), JsonResponse::HTTP_BAD_REQUEST, array('Content-Type' => 'application/json'));
}

My $request var contains the form data but the line $form->handleRequest($request) isn't working and the following error is displaying :

An exception occurred while executing 'INSERT INTO event (date_debut, date_fin, date_saisie, type, formateur_id, salle_id, promotion_id, module_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, "2016-08-08 12:50:50", null, null, null, null, null]:

All the data transmitted by the form are gone and I don't understand why neither how to do otherwise.

Any ideas to submit a form in AJAX with POST method?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 r语言神经网络自变量重要性分析
    • ¥15 基于双目测规则物体尺寸
    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢