A
A
Alexey Anisimov2016-02-01 14:23:29
symfony
Alexey Anisimov, 2016-02-01 14:23:29

Why is a form not being submitted in Symfony 3?

The form is rendered to the page through the Twig tag

{{ render(controller('AppBundle:Device:formSelectedDevice')) }}

Form code:
<?php

namespace AppBundle\Form;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SelectedDeviceType extends EntityBaseType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('device', EntityType::class, array(
                'label' => 'Выберите прибор',
                'class' => 'AppBundle\Entity\Device',
                'empty_data' => $options['data']['device']
            ))
        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
    }
}

Action code in controller:
public function formSelectedDeviceAction(Request $request)
    {
        $selectedDeviceId = $request->getSession()->get('device') ?: 1;

        $data = array();
        $data['device'] = $this->service->getEntity($selectedDeviceId);

        $form = $this->createForm(SelectedDeviceType::class, $data);
        $form->handleRequest($request);

        if($form->isValid()) {
            dump($form->getData());
            $request->getSession()->set('device', $form->getData()['device']->getId());
        }

        return $this->render('AppBundle:Device/block:selected-device.html.twig', array(
            'form' => $form->createView()
        ));
    }

The isValid condition is not met when the form is submitted,
The isSubmitted condition is not met when the form is submitted.
Tell me, what's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BoShurik, 2016-02-01
@AnisimovAM

The isSubmitted condition is not met when the form is submitted.

It looks like you are submitting the form with a GET, but $form->handleRequest($request);waiting for a POST request.
Or specify the method explicitly
$this->createForm(SelectedDeviceType::class, $data, array(
    'method' => 'GET'
));

or send POST
+ the form is rendered via a sub-request, then the submission goes to the current page:
$this->createForm(SelectedDeviceType::class, $data, array(
    'action' => $this->generateUrl('form_submit_route');
));

A
Alexey Skobkin, 2016-02-01
@skobkin

isValid condition fails on form submission,

Get a list of errors from the form and see what's wrong.
Did you add a submit button to the form?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question