1

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,

    ));

}
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Susan
  • 51
  • 1
  • 7
  • 1
    That's so rude!!! instead of helping poeple stack in problems, they come just to vote (-1) and go!!!!!!! – Susan Apr 02 '17 at 16:24
  • what do you mean with "get the info"? Everything posted is within the super global `$_POST` – DarkBee Apr 02 '17 at 16:27
  • 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. Apr 02 '17 at 16:27
  • Please post corresponding controller code. – Ollie in PGH Apr 02 '17 at 16:31
  • @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 Apr 02 '17 at 19:23
  • @ASOlivieri I posted the action code, but i think I should change/add something to twig to pass the fields content to the action. – Susan Apr 02 '17 at 19:25
  • @DarkBee if it were a form generated by the controller action, I could retrieve its data easily from the request, or by $form->getData(); But for this form I couldn't use those methods. – Susan Apr 02 '17 at 19:27
  • @Susan On this line: `$libprof=$request->request->get('profil');` you already get get selected option, right? What is your question? – Al.G. Apr 02 '17 at 20:00
  • this line return 'NULL' and also the other line : $iduser=$request->request->get('iduser'); – Susan Apr 02 '17 at 20:07
  • whenever i submit the form I get this error : An exception occurred while executing 'INSERT INTO userprof (id_user, id_profil) VALUES (?, ?)' with params [null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_user' cannot be null – Susan Apr 02 '17 at 20:09

1 Answers1

1

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.

Al.G.
  • 4,327
  • 6
  • 31
  • 56
  • Thank you for your help, but that didn't help, I've tried it before, and retried it now, but I got the same error :( – Susan Apr 02 '17 at 20:38
  • @Susan Then I suspect this may help you: http://stackoverflow.com/a/12142437/3132718 I.e. create a temporary form and bind it to the request. Then you can use it as if you had a normal form class. – Al.G. Apr 02 '17 at 20:42
  • @AI.G. Thank you soooooooo much i think this will help me. I'll try and post the result – Susan Apr 02 '17 at 20:53
  • No, it doesn't work! Do you think it's because I have dynamically genereted forms inside a for loop ??? – Susan Apr 02 '17 at 20:57
  • The way you generated the form doesn't matter if the data is sent properly. Try a rough `var_dump($_POST);` (in the controller where you receive the form) and see if the fields are sent correctly. If that's the case, you'd have to look at the controller. Otherwise, the twig generation of the form should be inspected. – Al.G. Apr 02 '17 at 21:00
  • It returns only the value of 'profil' but the other input field is not returned array(1) { ["profil"]=> string(7) "medecin" } – Susan Apr 02 '17 at 21:06
  • I just saw your `input` was `disabled`, you probably meant `readonly`. – Al.G. Apr 02 '17 at 21:08
  • OOOh hhh I didn't pay attention to that! But now when I retrieve the value using $request->request(''); I only get the value of the first input which is readable only, but the 'profil' value is {} – Susan Apr 02 '17 at 21:15
  • First look at `var_dump($_POST);` and only then at `$request`. See in `$_POST` what values have been sent. – Al.G. Apr 02 '17 at 21:32
  • I couldn't get the return of the var_dump; I got this error :An exception occurred while executing 'INSERT INTO userprof (id_user, id_profil) VALUES (?, ?)' with params ["222", {}]: Catchable Fatal Error: Object of class CNAM\CMSBundle\Entity\profil could not be converted to string – Susan Apr 02 '17 at 22:00
  • Put var_dump right in the beginning of the controller, before the sql query. – Al.G. Apr 02 '17 at 22:03
  • 1
    I fixed it by adding public function __toString() { return $this->libelle; } to my entity 'profil' – Susan Apr 02 '17 at 22:07