简体   繁体   English

Symfony2表单导致子请求

[英]Symfony2 form result in subrequest

I'm redering my header twig from the base twig as a sub request. 我正在从基础树枝中重新放置标头树枝作为子请求。 The following code line is in the base twig. 下面的代码行在基本树枝中。

{{ render(controller('MyBundle:Global:header')) }}

In the header twig there is a search form. 标题栏中有一个搜索表。 Not linked to any entity with the following definitions. 未链接到具有以下定义的任何实体。 This is the TopSearchType form definition. 这是TopSearchType表单定义。

        $builder
        ->add('searchKey', 'text', array('mapped' => false))
        ->add('save', 'submit');

In the sub controller I make the form and handle the validation result as follow. 在子控制器中,我制作表格并按以下方式处理验证结果。

    $form = $this->createForm(new TopSearchType());
    $form->handleRequest($request);

    if ($form->isValid()) {
        dump($form['searchKey']->getData());
    }

The form is generated but the result is not triggered? 生成表格但未触发结果? When I run the same code directly in the base twig it works fine. 当我直接在基本树枝中运行相同的代码时,它工作正常。

The thing is, your controller's action (Global:header) is being triggered by the render method, and when you submit it it's probably going to the action you're at on the page the header method is being rendered. 问题是,控制器的动作(Global:header)由render方法触发,提交时,它可能转到页面上您正在呈现的header方法。

You probably should create a separate action only to handle the post of the form, then use it as the form's action. 您可能应该只创建一个单独的操作来处理表单的帖子,然后将其用作表单的操作。

If you want to redirect the user to the current page you could add a hidden field storing the current URL, and then redirect him after the search. 如果要将用户重定向到当前页面,可以添加一个存储当前URL的隐藏字段,然后在搜索后将其重定向。

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

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