douzhang3822 2016-12-29 00:13
浏览 46

Symfony3表单构建器setAction不会重定向到路由

I have a form controlling the search bar in the websites navbar:

public function searchFormAction()
    {
        $form = $this->createFormBuilder()
            ->setAction($this->generateUrl('search'))
            ->setMethod('GET')
            ->add("value", TextType::class, array('label' => false))
            ->add("Search", SubmitType::class)
            ->getForm();

        return $this->render('components/search-form.html.twig', [
            "form" => $form->createView()
        ]);
    }

As you can see, the form has a specific action path to this function:

/**
     * @Route("/search", name="search")
     */
    public function searchAction(Request $request)
    {
        return $this -> render ("post/post-search.html.twig", [
            "value" =>  $request->query->get('value')
        ]);
    }

For now this shouldn't do much more than just display the value on the page.

The problem is that the website fails to redirect when the form is used

So when I put foo in the search, and click submit the path looks like this:

localhost:8000/page?form%5Bvalue%5D=foo&form%5BSearch%5D=&form%5B_token%5D=PsouIRAy2QaQ8j2XO_uYrs7PcaR6jyjQN3W3_xRMdgw

Moreover if I go to localhost:8000/search and try to put anything into the search bar, no value is printed.

Here is how the form is rendered:

//search-form.html.twig
<form class="navbar-form navbar-left">
    {{ form_start(form) }}
    <div class="form-group">
        {{ form_row(form.value) }}
    </div>
    {{ form_row(form.Search) }}
    {{ form_end(form) }}
</form>

And placed in the base navbar:

//base.html.twig
//...
 {{ render(controller(
                'AppBundle:Form:searchForm'
                )) }}
//...

Inspecting the element shows that the form tag has no action and method attributes

What could be the issue here and how can I fix it?

  • 写回答

1条回答 默认 最新

  • doujing6436 2016-12-30 18:47
    关注

    Fixed! Made a simple mistake in the twig file. Placed the form start inside html form tags, that way the submit button would send to an empty form.

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致