【问题标题】:Symfony embed edit controller not workingSymfony 嵌入编辑控制器不工作
【发布时间】:2018-06-01 15:23:43
【问题描述】:

我正在尝试在模式中使用编辑表单,但此编辑表单在上下文中不起作用。

当我使用 URI 地址在网页中直接尝试表单时它可以工作,但当我在我的页面上下文中尝试它时却不行。我会解释更多...

在我的网页中,我有一个render controller

    <div class="card">
        <div class="body">
            {{ render(controller('AppBundle:Classeur3:vacation', {'request':app.request , 'id': zone.id})) }}
        </div>
    </div>

这个控制器检索一些实体并显示如下表格:

<table id="sites-list" class="table table-bordered datatable">
    <thead>
        <tr>
            <th>Employé</th>
            <th>Heure d'entrée</th>
            <th>Heure de sortie</th>
            <th>Durée</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        {% for v in vacations %}
            <tr id="tr-vacation-{{ v.id }}">
                <td>{{ v.user }}</td>
                <td>{{ v.entryTime|date }}</td>
                <td>{{ v.exitTime|date }}</td>
                <td>{{ v.totalTime }}</td>
                <td style="width: 150px;">
                    <button onclick="$('#modal').modal({remote: '{{ path('edit_vacation', {'id': v.id}) }}' });" type="button" class="btn btn-circle waves-effect waves-circle waves-float" style="margin-right: 5px;">
                        <i class="material-icons">search</i>
                    </button>
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>

我的edit_vacation 资源是通过以下路线定义的:

edit_vacation:
    path:     /vacation/{id}/edit
    defaults: { _controller: AppBundle:Classeur3:editVacation }

这是我的控制器功能:

public function editVacationAction(Request $request, $id){

    $vacation       = $this->getDoctrine()->getRepository(Answ31Vacation::class)->find($id);
    $form           = $this->createForm(Answ31VacationType::class, $vacation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($vacation);
        $entityManager->flush();

        //Message
        $this->addFlash('success', 'Vacation mise à jours');

        //Redirect to Zone edit page
        return $this->redirectToRoute('zone_edit', array('id' => $vacation->getZone()->getId()));
    }

    return $this->render('AppBundle:Classeur3:edit_vacation.html.twig', array(
        "form" => $form->createView()
    ));
}

这是我的观点:

    <div class="modal-body">
        <div class="row">
            <div class="col-sm-12">
                {{ form(form) }}
            </div>
        </div> 
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">Fermer</button>
    </div>

模态显示完美,但是当我提交表单时,页面按计划重新加载但数据没有更新。 当我尝试直接在 URL 中调用我的资源( baseurl/vacation/1/edit )时,我有表单并且修改正在工作,当我按下提交按钮时,数据会更新。

有什么想法吗?

【问题讨论】:

  • 当您在没有其他表单的页面上打开模态框时是否有效?
  • 您的渲染语句似乎指向了一个假期方法,而您的代码似乎显示了 editVacationAction?但更重要的是,在浏览器中查看 html 源代码并查看表单发布到的位置。

标签: forms symfony controller render


【解决方案1】:

在您的editVacationAction 中,您应该尝试设置表单的目标操作,如下所示

$form = $this->createForm(Answ31VacationType::class, $vacation, ["action"=>$this->generateUrl('edit_vacation']);

但是当您提交时,页面会发生变化,并且您将失去模式而无需额外的 JavaScript。

【讨论】:

  • 我不知道为什么,但该操作并未自动添加到我的表单中。现在我正在使用您的解决方案。谢谢!
  • @Gauthier symfony 默认情况下不会将操作添加到表单中,期望它与当前页面的 url 相同。但是在您的情况下,您是在子请求中呈现表单,因此必须指定表单的目标操作,因为它与当前页面 url 不同。
猜你喜欢
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 2017-08-25
  • 1970-01-01
相关资源
最近更新 更多