symfony 提交表单后,如何删除URL中的锚?

dohp0rv5  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(222)

在Symfony 2.7中,我在索引页面上使用锚。该页面还包含一个表单,允许我过滤显示的元素。如果我使用锚点在页面内导航,则URL中会出现锚点。然后,如果我点击表单的提交按钮,过滤器就会被应用,页面就会被新内容刷新。问题是URL中的锚在这个过程中没有消失,刷新后,页面向下滚动到我访问的最后一个锚点。提交表格后,我如何重置URL?
以下是我的行动代码:

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $dailyPlanning = $em
        ->getRepository('AppBundle:Planning')
        ->findOneBy(array('day' => (new \DateTime)));
    //....//
    $filter = new Filter($param, $param2);
    $form = $this->createForm(new FilterType(), $filter);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $dailyPlanning->applyFilter($filter);
    }
    return $this->render('AppBundle:Workflow:index.html.twig', array(
      'planning' => $dailyPlanning,
      'form'     => $form->createView(),
      'filterHidden' => $filter->allChecked(),
    ));
}
11dmarpk

11dmarpk1#

有点老问题了...但迟到总比没有好:)在Symfony 5.4上测试

<?php
/**
 * @Route("/contac", name="contact", methods={"GET", "POST"})
*/
public function indexAction(Request $request)
{
    // ...
    $form = $this->createForm(new FilterType(), $filter, [
        'action' => $this->generateUrl('contact')
    ]);
    // ...

这将强制URL在提交后重新创建,原始URL将不会被使用。

相关问题