简体   繁体   English

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

[英]Symfony3 Form builder setAction won't redirect to route

I have a form controlling the search bar in the websites navbar: 我有一个表单来控制网站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: 因此,当我在搜索中添加foo并单击“提交”时,路径如下所示:

localhost:8000/page?form%5Bvalue%5D=foo&form%5BSearch%5D=&form%5B_token%5D=PsouIRAy2QaQ8j2XO_uYrs7PcaR6jyjQN3W3_xRMdgw 本地主机: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. 此外,如果我转到localhost:8000/search并尝试将任何内容放入搜索栏中,则不会输出任何值。

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 检查元素表明form标记没有动作和方法属性

What could be the issue here and how can I fix it? 这里可能是什么问题,我该如何解决?

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. 将表单开始放置在html表单标签内,这样,提交按钮将发送到空表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM