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_protectiontofalseinapp/config/config.ymland 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