Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my form is not submitted in symfony 4?

Tags:

forms

symfony

My controller acts as if I never click on the submit button.

My controller:

public function listAction(RegionsService $service)
{
    $regions = $service->getRegionList();

    $editForm = $this->createForm('App\Form\RegionListType');

    if ($editForm->isSubmitted())
    {
        dump('submitted');
        die();
    }

    if ($editForm->isSubmitted() && $editForm->isValid()) {

        $task = $editForm->getData();
        dump($task);
        die();
        ...
    }        

    return $this->render('parameter/region.list.html.twig', [
        'form'   => $editForm->createView(),
        'regions'   => $regions

    ]);
    ...

My form :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('regionsset', TextType::class, array(
                'required' => false))
            ->add('save', SubmitType::class, array(
                'attr' => array('class' => 'save')));
}

My view :

{{ form_start(form, {'action' : path('app_region_list')} ) }}
       {{ form_widget(form.regionsset, {'attr': {'class': 'foo'}}) }}
       {{ form_widget(form.save, { 'label': 'Save' }) }}
{{ form_end(form) }}

When I click on the submit button, the controller never goes into the first test if ($editForm->isSubmitted())

What did I miss ?

like image 245
Yves Avatar asked May 14 '18 09:05

Yves


1 Answers

You forgot to handle the request in your form. After creating the form, ($editForm), you have to handle the request as follows:

$editForm->handleRequest($request);

After that, the method isSubmitted() will return true.

like image 164
Manuel Lopez Avatar answered Oct 06 '22 21:10

Manuel Lopez