简体   繁体   English

Symfony2 表单提交后始终为空

[英]Symfony2 Form is always empty after submitting

I'm new with Symfony2 (2.4.4).我是 Symfony2 (2.4.4) 的新手。

I want to create a HTML layout which shows always a form on top (searchbar).我想创建一个 HTML 布局,它总是在顶部(搜索栏)显示一个表单。 I send the form via post and would like to redirect to another controller, which should pass the user input and generate an output.我通过 post 发送表单并希望重定向到另一个控制器,该控制器应该传递用户输入并生成输出。 I created a new function like this:我创建了一个这样的新函数:

public function searchFormAction(Request $request)
{
    //$defaultData = array('sstring' => 'Suche');
    $form = $this->createFormBuilder()
        ->add('fnr', 'hidden')
        ->add('sstring', 'search', array('label' => false))
        ->add('submit', 'submit', array('label' => 'suchen'))
        ->getForm();

    $form->handleRequest($request);

    if($request->isMethod('POST'))
    {
        return $this->redirect('SchmanEmployeeBundle:Employee:search', array(
            'sstring' => $form->get('sstring')->getData();
        ));
    }
    

    return $this->render('SchmanEmployeeBundle:Employee:searchForm.html.twig', array(
        'form' => $form->createView()
    ));
}

I extended my base layout (base.html.twig) and include the form with the render function我扩展了我的基本布局 (base.html.twig) 并包含具有渲染功能的表单

{% render(controller('SchmanEmployeeBundle:Employee:searchForm')) %}

This works fine and the form is always present in my layout.这很好用,表单总是出现在我的布局中。 The given HTML looks like this:给定的 HTML 如下所示:

<form name="form" method="post" action="/app_dev.php/">
<div><input type="search" id="form_sstring" name="form[sstring]" required="required"></div>
<div><button type="submit" id="form_submit" name="form[submit]">suchen</button></div>

Now I have 3 questions:现在我有3个问题:

  1. If I submit the form, I don't want to be redirected to the searchAction Controller.如果我提交表单,我不想被重定向到 searchAction 控制器。 This is because the $request->isMethod is always GET.这是因为 $request->isMethod 总是 GET。 Why?为什么? The form actions is post?表单操作是发布?

  2. In the Symfony Webtool the form section is also empty.在 Symfony Webtool 中,表单部分也是空的。 I see all form fields (sstring) and the data is always null.我看到所有表单字段(sstring)并且数据始终为空。 Where's the user input?用户输入在哪里?

  1. I guess its because you didnt specified, in your routing configuration, that the method of this function is POST.我猜是因为你没有在路由配置中指定这个函数的方法是 POST。
  2. Because the form never submitted to your function (your function want GET, but send POST)因为表单从未提交给您的函数(您的函数想要 GET,但发送 POST)
  3. Where is the last question?最后一个问题在哪里?

There is a great code to make your search function, it should work (sorry if you dont use annotation).有一个很棒的代码可以让你的搜索功能,它应该可以工作(对不起,如果你不使用注释)。 One good point, you can now use your searchType everywhere in your project, you should make your form like that instead of formbuilder into your controller.一个好处,您现在可以在项目中的任何地方使用您的 searchType,您应该像这样制作表单而不是 formbuilder 到您的控制器中。 Easier to read and to use.更易于阅读和使用。

Controller:控制器:

/**
 * To search something
 * 
 * @Route("/search", name="search")
 * @Template()
 */
public function searchAction()
{
    $form = $this->createForm(new searchType());
    $request = $this->get('request');

    if ($request->getMethod() == 'POST')
    {
        $form->bind($request);
        if ($form->isValid())
        {
            $informations = $form->get('search')->getData();
            //make things here
        }
    }
}

And here is the searchType class:这是 searchType 类:

class searchType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
          ->add('fnr', 'hidden')
          ->add('sstring', 'search', array('label' => false))
          ->add('submit', 'submit', array('label' => 'suchen'));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'yournamespace_searchType';
    }
}

First off, your form is set to be POST by default, so you should be good.首先,您的表单默认设置为 POST,所以您应该很好。 Second, you don't pass any data to be filled by your form, and I think you should.其次,您没有传递任何要由您的表单填写的数据,我认为您应该这样做。 Third, you don't check if the form is valid, which includes the test if it's submitted.第三,您不检查表单是否有效,其中包括测试是否已提交。 You should do this:你应该做这个:

$defaultData = array(); // No need for a class object, array is enough
$form = $this->createFormBuilder($defaultData)
    ->add('fnr', 'hidden')
    ->add('sstring', 'search', array('label' => false))
    ->add('submit', 'submit', array('label' => 'suchen'))
    ->getForm();

$form->handleRequest($request);

if($form->isValid())
{
    // Happens if the form is submitted
    return $this->redirect('SchmanEmployeeBundle:Employee:search', array(
        'sstring' => $form->get('sstring')->getData(); // TODO: This will probably produce an error, fix it
    ));
}

return $this->render('SchmanEmployeeBundle:Employee:searchForm.html.twig', array(
    'form' => $form->createView()
));

Also, I think you shouldn't worry about the form method because you don't have different implementations for other methods.另外,我认为您不应该担心 form 方法,因为您没有其他方法的不同实现。 This is the usual way the forms are handled in Symfony.这是在 Symfony 中处理表单的常用方式。 You should read on forms in detail before proceeding, the article is quite informative.在继续之前,您应该详细阅读表格,这篇文章内容丰富。

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

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