Symfony2:AJAX提交500错误“变量不存在”

时间:2015-12-17 00:07:29

标签: jquery ajax symfony symfony-forms

我有一个树枝视图,其中包含三种不同类型实体的几种形式:Area,Semester,RegularHour。以下示例是Area实体的edit.html.twig文件。

{% block body -%}
    <h1 class="page-header">HoursArea edit</h1>

    ...

    <div class="row">
        <div class="col-xs-12">
            <h2>Regular Hours</h2>
            {{ form(semester_form) }}
            Sunday: {{ form(day_0) }}
            Monday: {{ form(day_1) }}
            Tuesday: {{ form(day_2) }}
            Wednesday: {{ form(day_3) }}
            Thursday: {{ form(day_4) }}
            Friday: {{ form(day_5) }}
            Saturday: {{ form(day_6) }}
        </div>
    </div>
{% endblock %}
{% block documentReady %}
    $('.regularHours_form').submit(function(event){
        event.preventDefault();

        ajaxObject = {
            url: $("form").attr("action"),
            type: 'PUT',
            dataType: 'json',
            contentType: "application/json; charset=UTF-8",
            data: JSON.stringify({"openTime":$("#appbundle_hoursrgular_openTime", this).val(), "closeTime":$("#appbundle_hoursrgular_closeTime", this).val(), "is24hour":$("#appbundle_hoursrgular_is24Hour", this).is(':checked'), "isClosed":$("#appbundle_hoursrgular_isClosed", this).is(':checked')})
        };

        $.ajax(ajaxObject)
            .success(function(data,status,xhr) {
                    console.log( status );
            })
            .fail(function(data,status,xhr) {
                    console.log( status );
            })
            .always(function(data,status,xhr) {
                    console.log( status );
            });
    });  
    {{ parent() }}
{% endblock %}

当我提交其中一个“表单(day_X)”表单时,由于以下消息我收到500错误:

  

变量“semester_form”不存在于   AppBundle:HoursArea:第20行的edit.html.twig

我没有重新加载页面,所以我不太清楚为什么收到这条消息。

这是页面最初加载时的区域控制器消息(请注意,我首次加载时没有出现任何错误):

    #AreaController
/**
 * Displays a form to edit an existing HoursArea entity.
 *
 * @Route("/{id}/edit", name="hoursarea_edit")
 * @Method("GET")
 * @Template()
 */
    public function editAction($id)
    {
        $return = array(); //initialize the array of items to return to the view

        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppBundle:HoursArea')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find HoursArea entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        $service = $this->get('hours_service'); //the hours service

        $semesterForm = $service->createSemesterDropdown();
        $return['semester_form'] = $semesterForm;

        $semester = $em->getRepository('AppBundle:HoursSemester')->find(11);

        for($day = 0; $day < 7; $day++){
            $return['day_'.$day] = $this->getSemesterRegularHours($semester, $entity, $day);
        }

        $return['entity'] = $entity;
        $return['edit_form'] = $editForm->createView();
        $return['delete_form'] =$deleteForm->createView();

        return $return;
    }

这是生成每个小时表单的代码:

public function createEditForm(HoursRegular $entity)
{
    $form = $this->createForm(new HoursRegularType(), $entity, array(
        'action' => $this->generateUrl('hoursregular_update', array('id' => $entity->getId())),
        'method' => 'PUT',
        'attr' => array('class' => 'regularHours_form')
    ));

    $form->add('submit', 'submit', array('label' => 'Save Day'));

    return $form;
}

此外,每日小时表单提交给RegularHours控制器方法:

   /**
     * Edits an existing HoursRegular entity.
     *
     * @Route("/{id}", name="hoursregular_update")
     * @Method("PUT")
     */
    public function updateAction(Request $request, $id){

    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('AppBundle:HoursArea')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find HoursRegular entity.');
    }

    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    //$semesterForm 

    if ($editForm->isValid()) {
        $em->flush();

        $serializer = $this->get('serializer');
        $serialized = $serializer->serialize($entity, 'json');  

        return new JsonResponse("All is well!", 204);
    }

    return new JsonResponse("Oh no something went wrong!", 400);
}

显然我甚至没有达到更新代码,因为我收到500错误而不是204或400

1 个答案:

答案 0 :(得分:0)

由于XHTML 1.x表单仅支持GET和POST。 GET和POST是&#34;方法的唯一允许值&#34;属性。所以我建议你使用POST HTTP Verb而不是PUT。

希望这个帮助