Answer the question
In order to leave comments, you need to log in
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')) }}
<?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)
{
}
}
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()
));
}
Answer the question
In order to leave comments, you need to log in
The isSubmitted condition is not met when the form is submitted.
$form->handleRequest($request);
waiting for a POST request. $this->createForm(SelectedDeviceType::class, $data, array(
'method' => 'GET'
));
$this->createForm(SelectedDeviceType::class, $data, array(
'action' => $this->generateUrl('form_submit_route');
));
isValid condition fails on form submission,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question