Symfony2 Form is always empty after submitting

2019-07-17 20:07发布

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

2条回答
何必那么认真
2楼-- · 2019-07-17 20:59

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.

查看更多
淡お忘
3楼-- · 2019-07-17 21:03
  1. I guess its because you didnt specified, in your routing configuration, that the method of this function is POST.
  2. Because the form never submitted to your function (your function want GET, but send 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. 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:

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';
    }
}
查看更多
登录 后发表回答