1
votes

I have this form in twig, and I want to get the selected option and the value of the input field from a simple html form:

(PS: Don't say 'You'd better generate a form using the controller!' I know that, but I have a reason why I want to create a form in twig: because that allows me to create as many forms as I want using a for loop.)

I tried to pass arguments in the action path, but that didn't work.

<form action="{{ path('changeProf') }}" method="post" id="form">
    <section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 35%;top: 6vmin">
        <label style="display:inline-table;">
            <span> <input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/></span>
        </label>
    </section>
    <section class="col-lg-9 col-md-9 col-sm-9 col-xs-9"style="position: relative; left: 35%;top: 6vmin">
        <label style="display:inline-table;">
            <span>{% set k=1 %}
                <select id="profil">
                {% for prof in profArr %}
                    <option value="{{ prof }}"> {{ prof }} </option>
                {% endfor %}
                </select>
            </span>
        </label>
    </section>
    <input type="submit" class="btn btn-info btn-xs" style="position: relative;top:18vmin;left:-28%">
</form>

This is the action that handles the form :

/**
 * @Route("/profile/chProf",name="changeProf")
 * Method ("POST")
 * @Template()
 */
public function changeProfAction(Request $request)
{
    $session = new Session();
    $session->start();
    $search = $session->get('user');
    $gestAcces = $session->get('acces');
    $gestEtat = $session->get('etatUser');
    $gestCont = $session->get('contenu');
    $repMsg = $session->get('repMsg');
    $gestRec = $session->get('Reclam');
    $gestMess = $session->get('gestMess');
    $gestMp = $session->get('gestMp');


    if ($search == Null) {
        return $this->redirectToRoute('empty', array('search' => $search,
            'contenu' => $gestCont,
            'gestAcces' => $gestAcces,
            'gestEtat' => $gestEtat,
            'repMsg' => $repMsg,
            'gestRec' => $gestRec,
            'gestMess' => $gestMess,
            'gestMp' => $gestMp,
        ));
    }

    $em = $this
        ->getDoctrine()
        ->getManager();
    $reposit = $em->getRepository("CNAMCMSBundle:userprof");
    $rep = $em->getRepository("CNAMCMSBundle:profil");

    $userprof=new userprof();
    $libprof=$request->request->get('profil');
    $iduser=$request->request->get('iduser');

    $idprof=$rep->findOneBy(array('libelle'=>$libprof));
    $userprof->setIdUser($iduser);
    $userprof->setIdProfil($idprof);
    $em->persist($userprof);
    $em->flush();

    return $this->render('CNAMCMSBundle:Default:userProf.html.twig', array(
        'search'=>$search,
        'contenu'=>$gestCont,
        'gestAcces'=>$gestAcces,
        'gestEtat'=>$gestEtat,
        'repMsg'=>$repMsg,
        'gestRec'=>$gestRec,
        'gestMess'=>$gestMess,
        'gestMp'=>$gestMp,

    ));

}
1
That's so rude!!! instead of helping poeple stack in problems, they come just to vote (-1) and go!!!!!!! - Susan
what do you mean with "get the info"? Everything posted is within the super global $_POST - DarkBee
First. No one is obliged to help you. Secondly: a probable reason for downvote is the awfully formatted code (indentation) and question body (I tried to break this in parts to be more readable). I didn't downvote but I think as it currently stands your question is fine. - Al.G.
Please post corresponding controller code. - Ollie in PGH
@Al.G. I know no one is obliged to help me, but instead of just downvoting, they'd better to help and let me know what's wrong in my code. - Susan

1 Answers

1
votes

I think I found out what caused the error you receive.

  1. $request->request->get('profil'); returns NULL.
  2. This means, the form did not send a profil entry.
  3. Look where is the profil in the form:

    <input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/>
    
  4. There is no name attribute! Which is actually what is sent with the request. The name attribute, not the id. The id is used only for local styles and javascripts.

  5. The solution:

    <input type="text" value="{{ user.id }}" disabled="true" id="iduser" name="iduser" style="max-width: 18vmin;"/>
    
  6. Do the same for profil

Hope this helps.