This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Randomly generated form - error on submit

New topics about Symfony 2 should go here

Randomly generated form - error on submit

by Mesta » Thu Jun 04, 2015 6:44 pm


I'm new to Symfony, and trying to build my very first application with it.
I recently developed a form composed by a single two elements choices field. Those elements are random rows caught in database.

Here is the controller method that instantiate this form.

Code: Select all
public function versusAction(Request $request)
        $session = $request->getSession();

        $retour = array();
        $retour["joueurs"] = array();

        $em = $this->getDoctrine()->getManager();
        $joueurs = $em->getRepository('LGELmashBundle:Joueur')->findRandomEligibleJoueurs(2);

        $categorie = $em->getRepository('LGELmashBundle:Categorie')->findAll();
        $retour["categorie"] = $categorie[0];

        $form = $this->createFormBuilder()
            ->add('vote', 'choice', array(
                'choices' => array(
                    $joueurs[0]->getId() => "" . $joueurs[0]->getPseudo() . " (" .  $joueurs[0]->getId() .")",
                    $joueurs[1]->getId() => "" . $joueurs[1]->getPseudo() . " (" .  $joueurs[1]->getId() .")",
                'constraints' => new Choice(
                        'choices' => array($joueurs[0]->getId(), $joueurs[1]->getId()),
                        'message' => 'Choisissez un joueur.'
                'expanded'  => true,
                'label'     => ' ',
                'multiple'  => false,
                'required'  => true,
                'empty_value' => null))
            ->add('categorie', 'hidden', array( 'data' => $retour["categorie"]->getId() ) )
            ->add('submit', 'submit', array('label' => 'Voter') )


        if ($form->isSubmitted()) {
            $choix_1    = $session->get("vote")[0];
            $choix_2    = $session->get("vote")[1];

            $categorie  = $form->get("categorie")->getData();
            $vote       = $form->get("vote")->getData();


                'Votre vote a été pris en compte! Il vous reste n jetons de vote.'

        $retour["form"] = $form->createView();
        return $retour;

The reason I'm looking for help is when I submit this form, i randomly got a TransformationFailedException. I recently understand that this error is triggered when the choice I've made, available in the Request object, is not a part of the new generated form.

My questions are :
is there something that I'm doing/understand badly ? Meaning not in Symfony spirit.
I thought I could try to manage the POST request in an other method's controller, but it seems to be a wrong choice... On your opinion, what are the solutions to handle this problem ?

I would be greatful to any help and helpers !
Junior Member
Posts: 1
Joined: Wed Jun 03, 2015 7:27 pm