title: A form can only be submitted once 500 Internal Server Error - AlreadySubmittedException
body: <p>I am trying to create a login/registration form using FOSUserBundle. After logging in the user gets a homepage. wherein he has to choose two different events for 2 time slots from two radio button type options and hit submit. Also if a user has registered already and logs in, then he can see his previously selected choices. Also he can change them. When I created the homepage from inside the controller, the code worked fine. </p>
  
  <p>Here is the controller code:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use AppBundle\Entity\events;
  //use AppBundle\Entity\eventtype;
  use AppBundle\Entity\users;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  
  class DefaultController extends Controller {
  
      /**
       * @Route("/home", name="homepage")
       */
      public function indexAction(Request $request) {
  
          $events = new events();
  
          $greet = 'Welcome to the Birthday Party registration!';
          $selection1 = '';
          $selection2 = '';
  
          $et1 = 0;
          $et2 = 0;
  
           //get the events repository
          $repository = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:events');
  
          //get the user_id of the logged in user
          $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
          $events-&gt;setUser($user);
          $x = $events-&gt;getUser()-&gt;getID();
  
          //check if the user has already registered or not
          $y = $repository-&gt;findOneBy(array('user' =&gt; $x));
  
          //If the user has registered already, set the data value for the form 
          if($y){
              $et1 = $y-&gt;getET1();
              $et2 = $y-&gt;getET2();
          }
  
          //create form
          $form = $this-&gt;createFormBuilder($events)
                  -&gt;add('eT1', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'Poker' =&gt; 1,
                          'Chess' =&gt; 2,
                          'Cricket' =&gt; 3,
                          'Marbles' =&gt; 4,
                          'Football' =&gt; 5,
                      ),
                      'choices_as_values' =&gt; true,
                      'expanded' =&gt; true,
                      'multiple' =&gt; false,
                      'label' =&gt; 'Choose After Breakfast Event',
                      'data' =&gt; $et1
                  ))
                  -&gt;add('eT2', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'Poker' =&gt; 1,
                          'Chess' =&gt; 2,
                          'Cricket' =&gt; 3,
                          'Marbles' =&gt; 4,
                          'Football' =&gt; 5,
                      ),
                      'choices_as_values' =&gt; true,
                      'expanded' =&gt; true,
                      'multiple' =&gt; false,
                      'label' =&gt; 'Choose After Snacks Event',
                      'data' =&gt; $et2
                  ))
                  -&gt;add('save', SubmitType::class, array('label' =&gt; 'Submit'))
                  -&gt;getForm();
  
          //retrieve the choices array for eT1 and eT2
          $eT1Choices = $form-&gt;get('eT1')-&gt;getConfig()-&gt;getOption('choices');
          $eT2Choices = $form-&gt;get('eT2')-&gt;getConfig()-&gt;getOption('choices');
  
          //intialize the eventname variables
          $eT1Name = '';
          $eT2Name = '';
  
          if ($y) {
  
              //If the user has registered already, display his previously selected options
              $selection1 = 'Your After Breakfast event:';
              $selection2 = 'Your After Snacks event:';
  
              //set the eventname based on the value of et1
              foreach ($eT1Choices as $key =&gt; $value) {
                  if ($et1 == $value) {
                      $eT1Name = $key;
                  }
              }
  
              //set the eventname based on the value of et2
              foreach ($eT2Choices as $key =&gt; $value) {
                  if ($et2 == $value) {
                      $eT2Name = $key;
                  }
              }
          }
  
          //after submission
          if ($request-&gt;isMethod('POST')) {
              $form-&gt;submit($request);
  
              //retrieve maxlimit parameters from parameters.yml
              $maxPoker = $this-&gt;container-&gt;getParameter('pokermaxlimit');
              $maxChess = $this-&gt;container-&gt;getParameter('chessmaxlimit');
              $maxCricket = $this-&gt;container-&gt;getParameter('cricketmaxlimit');
              $maxMarbles = $this-&gt;container-&gt;getParameter('marblesmaxlimit');
              $maxFootball = $this-&gt;container-&gt;getParameter('footballmaxlimit');
  
              //initialize $eventMaxLim
              $eventMaxLim = 0;
  
              //retrieve form data
              $formData = $form-&gt;getData();
  
              $ET1 = $formData-&gt;getET1();
              $ET2 = $formData-&gt;getET2();
  
              $selection1 = 'Your After Breakfast event:';
              $selection2 = 'Your After Snacks event:';
  
              //set the eventname based on the value of eT1
              foreach ($eT1Choices as $key =&gt; $value) {
                  if ($ET1 == $value) {
                      $eT1Name = $key;
                  }
              }
  
              //set the eventname based on the value of eT2
              foreach ($eT2Choices as $key =&gt; $value) {
                  if ($ET2 == $value) {
                      $eT2Name = $key;
                  }
              }
  
              //check to see if the user has registered the same event for eT1 and eT2
              if ($ET1 == $ET2) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'You have chosen same events for both time slots! Please choose different ones.');
              }
  
              //check to see how many users have registered for the opted event(eT1)
              $query1 = $repository-&gt;createQueryBuilder('p')
                      -&gt;select('count(p)')
                      -&gt;where('p.eT1 = :eT1')
                      -&gt;setParameter('eT1', $ET1)
                      -&gt;getQuery();
              $a = $query1-&gt;getSingleScalarResult();
  
              //set the $eventMaxLim based on the chosen event for eT1
              if ($ET1 == 1) {
                  $eventMaxLim = $maxPoker;
              } else if ($ET1 == 2) {
                  $eventMaxLim = $maxChess;
              } else if ($ET1 == 3) {
                  $eventMaxLim = $maxCricket;
              } else if ($ET1 == 4) {
                  $eventMaxLim = $maxMarbles;
              } else if ($ET1 == 5) {
                  $eventMaxLim = $maxFootball;
              }
  
              //check to see if the after breakfast event (eT1) is full (ie.has reached the maxlimit)
              if ($a &gt;= $eventMaxLim) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'choose another After Breakfast event, this one is full');
              }
  
              //check to see how many users have registered for the opted event(eT2)
              $query2 = $repository-&gt;createQueryBuilder('p')
                      -&gt;select('count(p)')
                      -&gt;where('p.eT2 = :eT2')
                      -&gt;setParameter('eT2', $ET2)
                      -&gt;getQuery();
              $b = $query2-&gt;getSingleScalarResult();
  
              //set the $eventMaxLim based on the chosen event for eT2
              if ($ET2 == 1) {
                  $eventMaxLim = $maxPoker;
              } else if ($ET2 == 2) {
                  $eventMaxLim = $maxChess;
              } else if ($ET2 == 3) {
                  $eventMaxLim = $maxCricket;
              } else if ($ET2 == 4) {
                  $eventMaxLim = $maxMarbles;
              } else if ($ET2 == 5) {
                  $eventMaxLim = $maxFootball;
              }
  
              //check to see if the after snacks event (eT2) is full (ie.has reached the maxlimit)
              if ($b &gt;= $eventMaxLim) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'choose another After Snacks event, this one is full');
              }
  
              if (($a &lt; $eventMaxLim) &amp;&amp; ($b &lt; $eventMaxLim) &amp;&amp; ($ET1 != $ET2)) {
                  if ($form-&gt;isValid()) {
  
                      //get the entity manager
                      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
                      // If the user is registering for the first time (execute the Insert query)
                      if (!$y) {
                          $em-&gt;persist($events);
                          $em-&gt;flush();
                          //return $this-&gt;redirectToRoute('homepage');
                      }
  
                      //If the user has registered already and want change his registered events (execute the Update query)
                      else {
                          $y-&gt;setET1($ET1);
                          $y-&gt;setET2($ET2);
                          $em-&gt;persist($y);
                          $em-&gt;flush();
                          //return $this-&gt;redirectToRoute('homepage');
                      }
                  }
              }
          }
  
          return $this-&gt;render('default/index.html.twig', array(
                      'form' =&gt; $form-&gt;createView(),
                      'greet' =&gt; $greet,
                      'selection1' =&gt; $selection1,
                      'eT1Name' =&gt; $eT1Name,
                      'selection2' =&gt; $selection2,
                      'eT2Name' =&gt; $eT2Name,
          ));
      }
  
  }
  </code></pre>
  
  <p>Below is the events entity:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  //use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * events
   *
   * @ORM\Table(name="events")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\eventsRepository")
   */
  class events {
  
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @var int
       *
       * @ORM\Column(name="ET1", type="integer")
       */
      protected $eT1;
  
      /**
       * @var int
       *
       * @ORM\Column(name="ET2", type="integer")
       */
      protected $eT2;
  
      /**
       * @ORM\OneToOne(targetEntity="users", inversedBy="event")
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
       */
          protected $user;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId() {
          return $this-&gt;id;
      }
  
      /**
       * Set user
       *
       * @param users $user
       * @return events
       */
      public function setUser($user) {
          $this-&gt;user = $user;
  
          return $this;
      }
  
      /**
       * Get user
       *
       * @return events
       */
      public function getUser() {
          return $this-&gt;user;
      }
  
  
      /**
       * Set eT1
       *
       * @param integer $eT1
       * @return events
       */
      public function setET1($eT1) {
          $this-&gt;eT1 = $eT1;
  
          return $this;
      }
  
      /**
       * Get eT1
       *
       * @return integer 
       */
      public function getET1() {
          return $this-&gt;eT1;
      }
  
      /**
       * Set eT2
       *
       * @param integer $eT2
       * @return events
       */
      public function setET2($eT2) {
          $this-&gt;eT2 = $eT2;
  
          return $this;
      }
  
      /**
       * Get eT2
       *
       * @return integer 
       */
      public function getET2() {
          return $this-&gt;eT2;
      }
  
  }
  </code></pre>
  
  <p>But when I shifted the code for Form creation in the eventsType.php, The following error has been showing up - A form can only be submitted once 500 Internal Server Error - AlreadySubmittedException
  Here is the new controller code:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use AppBundle\Entity\events;
  use AppBundle\Form\eventsType;
  use AppBundle\Entity\users;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  
  class DefaultController extends Controller {
  
      /**
       * @Route("/home", name="homepage")
       */
      public function indexAction(Request $request) {
  
          $greet = 'Welcome to the Birthday Party registration!';
          $selection1 = '';
          $selection2 = '';
  
          //get the events repository
          $repository = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:events');
  
          //get the user_id of the logged in user
          $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser()-&gt;getID();
  
          //check if the user has already registered or not
          $regEvents = $repository-&gt;findOneBy(array('user' =&gt; $user));
  
          $events = new events();
  
          //create form
          $form = $this-&gt;createForm(new \AppBundle\Form\eventsType($regEvents), $events);
  
          $form-&gt;handleRequest($request);
  
          //retrieve the choices array for eT1 and eT2
          $eT1Choices = $form-&gt;get('eT1')-&gt;getConfig()-&gt;getOption('choices');
          $eT2Choices = $form-&gt;get('eT2')-&gt;getConfig()-&gt;getOption('choices');
  
          //intialize the eventname variables
          $eT1Name = '';
          $eT2Name = '';
  
          if ($regEvents) {
  
              $et1 = $regEvents-&gt;getET1();
              $et2 = $regEvents-&gt;getET2();
  
              //If the user has registered already, display his previously selected options
              $selection1 = 'Your After Breakfast event:';
              $selection2 = 'Your After Snacks event:';
  
              //set the eventname based on the value of et1
              foreach ($eT1Choices as $key =&gt; $value) {
                  if ($et1 == $value) {
                      $eT1Name = $key;
                  }
              }
  
              //set the eventname based on the value of et2
              foreach ($eT2Choices as $key =&gt; $value) {
                  if ($et2 == $value) {
                      $eT2Name = $key;
                  }
              }
          }
  
          //after submission
          if ($request-&gt;isMethod('POST')) {
              $form-&gt;submit($request);
  
              //First check the value entered by the user
              if ($events-&gt;getET1() == null || $events-&gt;getET2() == null) {
                  //User did not choose both the events
                  $this-&gt;container-&gt;get('session')-&gt;getFlashBag()-&gt;add('error', 'Oh oh! It is mandatory to choose an option for all the events');
                  //return array('form' =&gt; $form-&gt;createView());
              }
  
              //retrieve maxlimit parameters from parameters.yml
              $maxPoker = $this-&gt;container-&gt;getParameter('pokermaxlimit');
              $maxChess = $this-&gt;container-&gt;getParameter('chessmaxlimit');
              $maxCricket = $this-&gt;container-&gt;getParameter('cricketmaxlimit');
              $maxMarbles = $this-&gt;container-&gt;getParameter('marblesmaxlimit');
              $maxFootball = $this-&gt;container-&gt;getParameter('footballmaxlimit');
  
              //initialize $eventMaxLim
              $eventMaxLim1 = 0;
              $eventMaxLim2 = 0;
  
              //retrieve form data
              $formData = $form-&gt;getData();
  
              $ET1 = $formData-&gt;getET1();
              $ET2 = $formData-&gt;getET2();
  
              $selection1 = 'Your After Breakfast event:';
              $selection2 = 'Your After Snacks event:';
  
              //set the eventname based on the value of eT1
              foreach ($eT1Choices as $key =&gt; $value) {
                  if ($ET1 == $value) {
                      $eT1Name = $key;
                  }
              }
  
              //set the eventname based on the value of eT2
              foreach ($eT2Choices as $key =&gt; $value) {
                  if ($ET2 == $value) {
                      $eT2Name = $key;
                  }
              }
  
              //check to see if the user has registered the same event for eT1 and eT2
              if ($ET1 == $ET2) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'You have chosen same events for both time slots! Please choose different ones.');
              }
  
              //check to see how many users have registered for the opted event(eT1)
              $query1 = $repository-&gt;createQueryBuilder('p')
                      -&gt;select('count(p)')
                      -&gt;where('p.eT1 = :eT1')
                      -&gt;setParameter('eT1', $ET1)
                      -&gt;getQuery();
              $a = $query1-&gt;getSingleScalarResult();
  
              //set the $eventMaxLim based on the chosen event for eT1
              if ($ET1 == 1) {
                  $eventMaxLim1 = $maxPoker;
              } else if ($ET1 == 2) {
                  $eventMaxLim1 = $maxChess;
              } else if ($ET1 == 3) {
                  $eventMaxLim1 = $maxCricket;
              } else if ($ET1 == 4) {
                  $eventMaxLim1 = $maxMarbles;
              } else if ($ET1 == 5) {
                  $eventMaxLim1 = $maxFootball;
              }
  
  //            var_dump($eventMaxLim1);
  //            exit;
              //check to see if the after breakfast event (eT1) is full (ie.has reached the maxlimit)
              if ($a &gt;= $eventMaxLim1) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'choose another After Breakfast event, this one is full');
              }
  
              //check to see how many users have registered for the opted event(eT2)
              $query2 = $repository-&gt;createQueryBuilder('p')
                      -&gt;select('count(p)')
                      -&gt;where('p.eT2 = :eT2')
                      -&gt;setParameter('eT2', $ET2)
                      -&gt;getQuery();
              $b = $query2-&gt;getSingleScalarResult();
  
              //set the $eventMaxLim based on the chosen event for eT2
              if ($ET2 == 1) {
                  $eventMaxLim2 = $maxPoker;
              } else if ($ET2 == 2) {
                  $eventMaxLim2 = $maxChess;
              } else if ($ET2 == 3) {
                  $eventMaxLim2 = $maxCricket;
              } else if ($ET2 == 4) {
                  $eventMaxLim2 = $maxMarbles;
              } else if ($ET2 == 5) {
                  $eventMaxLim2 = $maxFootball;
              }
  
              //check to see if the after snacks event (eT2) is full (ie.has reached the maxlimit)
              if ($b &gt;= $eventMaxLim2) {
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;set('error', 'choose another After Snacks event, this one is full');
              }
  
              if (($a &lt; $eventMaxLim1) &amp;&amp; ($b &lt; $eventMaxLim2) &amp;&amp; ($ET1 != $ET2) &amp;&amp; ($events-&gt;getET1() == null ||
                      $events-&gt;getET2() == null)) {
                  if ($form-&gt;isValid()) {
  
                      //get the entity manager
                      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
                      // If the user is registering for the first time (execute the Insert query)
                      if (!$regEvents) {
                          $events-&gt;setUser($user);
                          $events-&gt;setET1($ET1);
                          $events-&gt;setET2($ET2);
                          $em-&gt;persist($events);
                          $em-&gt;flush();
                          //return $this-&gt;redirectToRoute('homepage');
                      }
  
                      //If the user has registered already and want change his registered events (execute the Update query)
                      else {
                          $events-&gt;setET1($ET1);
                          $events-&gt;setET2($ET2);
                          $em-&gt;persist($events);
                          $em-&gt;flush();
                          //return $this-&gt;redirectToRoute('homepage');
                      }
                  }
              }
          }
  
          return $this-&gt;render('default/index.html.twig', array(
                      'form' =&gt; $form-&gt;createView(),
                      'greet' =&gt; $greet,
                      'selection1' =&gt; $selection1,
                      'eT1Name' =&gt; $eT1Name,
                      'selection2' =&gt; $selection2,
                      'eT2Name' =&gt; $eT2Name,
          ));
      }
  
  }
  </code></pre>
  
  <p>Below is the eventsType.php:</p>
  
  <pre><code>&lt;?php 
  
  namespace AppBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  
  class eventsType extends AbstractType {
  
      protected $events;
  
      public function __construct($events) {
          $this-&gt;events = $events;
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options) {
  
              if (!empty($this-&gt;events)){
             if($this-&gt;events-&gt;getET1() == null){
                  $et1 = '';   
             }
             else {
                 $et1 = $this-&gt;events-&gt;getET1();
             }
             if($this-&gt;events-&gt;getET2() == null){
                  $et2 = '';   
             }
             else {
                 $et2 = $this-&gt;events-&gt;getET2();
             }
              }
              else {
             $et1 = '';
             $et2 = '';
              }
  
          $builder-&gt;add('eT1', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'Poker' =&gt; 1,
                          'Chess' =&gt; 2,
                          'Cricket' =&gt; 3,
                          'Marbles' =&gt; 4,
                          'Football' =&gt; 5,
                      ),
                      'choices_as_values' =&gt; true,
                      'expanded' =&gt; true,
                      'multiple' =&gt; false,
                      'label' =&gt; 'Choose After Breakfast Event',
                      'data' =&gt; $et1,
  //                    'mapped' =&gt; $map1,
                  ))
                  -&gt;add('eT2', ChoiceType::class, array(
                      'choices' =&gt; array(
                          'Poker' =&gt; 1,
                          'Chess' =&gt; 2,
                          'Cricket' =&gt; 3,
                          'Marbles' =&gt; 4,
                          'Football' =&gt; 5,
                      ),
                      'choices_as_values' =&gt; true,
                      'expanded' =&gt; true,
                      'multiple' =&gt; false,
                      'label' =&gt; 'Choose After Snacks Event',
                      'data' =&gt; $et2,
  //                    'mapped' =&gt; $map2,
                  ))
                  -&gt;add('save', SubmitType::class, array('label' =&gt; 'Submit'));
      }
  
      public function configureOptions(OptionsResolver $resolver) {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\events',
          ));
      }
  
  }
  </code></pre>