提交后Symfony2表单总是空的 [英] Symfony2 Form is always empty after submitting

查看:151
本文介绍了提交后Symfony2表单总是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望创建一个HTML布局,在顶部显示一个表单(searchbar)。我通过发布发送表单,并希望重定向到另一个控制器,这应该通过用户输入genarate输出。我创建了一个像这样的新函数:

$ p $ 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();
));


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

我扩展了我的基本布局(base.html.twig)并包含表单使用渲染函数

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

这个工作正常,表单总是出现在我的布局中。给定的HTML如下所示:

 < form name =formmethod =postaction =/ app_dev。 PHP /> 
< div>< input type =searchid =form_sstringname =form [sstring]required =required>< / div>
< div>< button type =submitid =form_submitname =form [submit]> suchen< / button>< / div>




现在我有3个问题。 :)


  1. 如果我提交表单,我不会被重定向到searchAction控制器。这是因为$ request-> isMethod总是GET。为什么?表单操作是post?


  2. 在Symfony Webtool中,表单部分也是空的。我看到所有表单字段(sstring)和数据都是空的。



  3. 请帮助我
    谢谢



      $ defaultData = array(); //不需要类对象,数组就足够了
    $ 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())
    {
    //如果表单已提交,会发生
    return $ this-> redirect('SchmanEmployeeBundle :Employee:search',array(
    'sstring'=> $ form-> get('sstring') - > getData(); // TODO:这可能会产生错误,修正它
    ));

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

    另外,我认为你不应该担心表单方法,因为你没有不同的实现用于其他方法。这是表单在Symfony中处理的常用方式。在继续阅读之前,您应详细阅读表单,文章内容丰富。


    Hi @ all im new with Symfony2 (2.4.4).

    I want to create a HTML layout wich shows allways a form on top (searchbar). I send the form via post and would like to redirect to another controller, which should pass the user input an genarate an output. 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

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

    This works fine and the form is allways present in my layout. The given HTML looks like this:

    <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. :)

    1. If i submit the form, i dont be redirected to the searchAction Controller. This is because the $request->isMethod is allways GET. Why? The form actions is post?

    2. In the Symfony Webtool the form section is also empty. I see all form fields (sstring) and the data is allways null. Where's the user input?

    Pls help me Thanks

    解决方案

    First off, your form is set to be POST by default, so you should be good. 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. This is the usual way the forms are handled in Symfony. You should read on forms in detail before proceeding, the article is quite informative.

    这篇关于提交后Symfony2表单总是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆