1

My project worked fine on my local computer. But once I uploaded it on an OVH server I faced some problems when I submit any form I have (such as registration of FOSUserBundle and also my own forms).

The problem is when I submit the form I have an invalid CSRF token.

I tried many solutions:

  • I added {{ form_rest(form) }} to my twig files.

  • I changed csrf_protection to false in app/config/config.yml and then the request was submited without any field from the form.

Note : I tried with symfony 2.4 and 2.3 but the problem persists.

My twig:

<form id="modifier_form" name="modifier_form" method="post" {{ form_enctype(form) }}>
    <div id="form_prenom">{{ form_row(form.prenom) }}</div>
    <div id="form_nom">{{ form_row(form.nom) }}</div>

    <div id="form_situation_pro">
        <label>Situation professionnelle</label>
        <select name="optone" 
                id="pays_select"
                onchange="setOptions(document.modifier_form.optone.options[document.modifier_form.optone.selectedIndex].value);">
                    <option value=" " selected="selected"> </option>
                    {% for a in pays  %}
                        <option value="{{ a.getId }}">{{ a.getName }}</option>
                    {% endfor %}
        </select>
    </div>
    <div id="form_situation_pro_sous">
        <label></label>
        <select name="opttwo" id="region_select">
            <option value=" " selected="selected"></option>
        </select>
    </div>

    {{ form_widget(form) }}
    {{ form_restt(form) }}
    <input type="submit" value="Enregistrer" />

My Controller

public function modifierAction(){
    $connecteduser =$this->getUser();
    $fich=$connecteduser->getFichsig();
    //si l'utulisateur a deja une fiche sinon il faut le remplire
    if(!$fich){
        $fich = new Fichsig;
        $connecteduser->setFichsig($fich);
    }

    $myuser=$this->getDoctrine()
        ->getManager()
        ->getRepository('TS\InscriptionBundle\Entity\User')->findByEmail($connecteduser->getEmail());

    $form = $this->createForm(new UserType($this->getDoctrine()->getManager()), $connecteduser);

    $request = $this->getRequest();
    if ($request->isMethod('POST')) {
        $form->bindRequest($request);
        if ($form->isValid()) {
            //get parametres
            ////
            $connecteduser->setFichsig($fich);
            $em = $this->getDoctrine()->getManager();
            $em->merge($connecteduser);
            $em->flush();
            return $this->redirect($this->generateUrl('ts_inscription_profil'));
        }
    }
    // On passe la méthode createView() du formulaire à la vue afin qu'elle puisse afficher le formulaire toute seule
    return $this->render('TSInscriptionBundle:Logged:modifier_logged_layout.html.twig', array(
        'form' => $form->createView(),
        'pays'=>$this->pays(),
        'region'=>$this->region(),

    ));
}

My config.yml

framework:
#esi:             ~
translator:      { fallback: "%locale%" }
secret:          "%secret%"
router:
    resource: "%kernel.root_dir%/config/routing.yml"
    strict_requirements: ~
form:            ~
csrf_protection: ~
validation:      { enable_annotations: true }
templating:
    engines: ['twig']
    #assets_version: SomeVersionScheme
default_locale:  "%locale%"
trusted_hosts:   ~
trusted_proxies: ~
session:
    # handler_id set to null will use default session handler from php.ini
    handler_id:  ~
fragments:       ~
http_method_override: true
1
  • can you post some PHP, twig and corresponding genrated HTML? Commented Mar 29, 2014 at 21:48

3 Answers 3

1

You wrote

{{ form_restt(form) }}

It should be like:

{{ form_rest(form) }}

and also remove {{ form_widget(form) }}. It used to render part of the form. As you have already rendered a field form.prenom. form_rest will render the rest of the fields.

Happy Coding...

Sign up to request clarification or add additional context in comments.

10 Comments

Sorry I made a mistake in copy for {{ form_restt(form) }} I also removed {{ form_widget(form) }} but still nothing changed
Could you try to update your form type's setDefaultOptions function like here and let me know the result
I already tried it and retried it again. The result is that the form is rendred with empty values and it updates my databases with empty values
So this disable the csrf validation! Now could you change the $form->bindRequest($request); to $form->handleRequest($request);
now when I submit, the form is reseted (the redirect don't work) with and without 'csrf_protection' => false
|
0

you can simply let the twig render it for you using {{ form(form) }} once starting to use widget, rows and labels then you will have to use {{ form_start(form) }} and after the submit button and before the {{form_end(form) }} you should put {{ form_rest(form) }}

I think you are missing the form_end and correct the form_rest as it has a spelling mistake

Comments

-1

Set the form end if you use symfony 2.3

{{ form_end(form, {'render_rest': false}) }}

2 Comments

I would like to remind you that it work fine on local( dev and prod mode).
@Yakiv Mospan , it's a twig code and not a java. it's {{ ... }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.