Yves
Yves

Reputation: 461

Why my form is not submitted in symfony 4?

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 ?

Upvotes: 5

Views: 7123

Answers (2)

Shaithya Kannan
Shaithya Kannan

Reputation: 142

Try this code. It will resolve the problem.

use Symfony\Component\HttpFoundation\Request;
.
.
.
public function listAction(Request $request, RegionsService $service )
{
    $regions = $service->getRegionList();

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

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

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

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

    ]);
    ...

Upvotes: -2

Manuel Lopez
Manuel Lopez

Reputation: 378

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.

Upvotes: 12

Related Questions