简体   繁体   中英

Symfony2 Form is always empty after submitting

I'm new with Symfony2 (2.4.4).

I want to create a HTML layout which shows always 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 and generate 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 always 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 don't want to be redirected to the searchAction Controller. This is because the $request->isMethod is always 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 always null. 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.
  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';
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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