1

I've been trying to create a simple form in symfony but each time I try to submit I get the following error:

 ERROR: The CSRF token is invalid. Please try to resubmit the form.

After surfing on the Internet and reducing the code to almost empty. I still get that error. Most of the people who I've seen asking for that solved the error using the following twig code

{{ form_rest(form) }}

The problem is that I'm using it, it's like when I bind the request it doesn't do it correctly. I don't know what else can I do.

This is my small twig template:

<div><h2>Insert new activity</h2></div>

<div>
<form id="new-activity" action="{{ path('create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_rest(form) }}       
    <p>
        <button type="submit">Submit</button>
    </p>      
</form>
</div>

As you can see it is pretty small code. This is my form render code:

/**
 * Displays a form to create a new Activity entity.
 *
 * @Route("/new", name="sucr_new")
 * @Template()
 */

public function newAction() {
$initialConfig = new SucrConfiguration();
$finalConfig = new SucrConfiguration();
$activity = new SucrActivity();
$data = array("activity" =>$activity, 
                  "initialConfig" => $initialConfig,
                  "finalConfig" => $finalConfig);
$form = $this->createForm(new ActivityType(), $data);

return array(
    'data' => $data,
    'form' => $form->createView()               
);
}

And this is the code that should handle the submition:

/**
 * Displays a form to create a new Activity entity.
 *
 * @Route("/create", name="create")
 * @Method("post")
 * @Template("EusocSucrBundle:Sucr:new.html.twig")
 */
 public function createAction() {
$initialConfig = new SucrConfiguration();
$finalConfig = new SucrConfiguration();
$activity = new SucrActivity();
$data = array("activity" =>$activity, 
                  "initialConfig" => $initialConfig,
                  "finalConfig" => $finalConfig);
$form = $this->createForm(new ActivityType(), $data);
if ($this->getRequest()->getMethod() == 'POST') {
    $form->bindRequest($this->getRequest());

    if ($form->isValid()) {
        return $this->redirect($this->generateUrl('sucr_show', array('id' => 1)));
    }
    var_dump($form->getErrorsAsString());

}


return array(
    'data' => $data,
    'form' => $form->createView()               
);         
}

Also note that I can see the hidden token in my browser.

Any ideas?

Daerum
  • 111
  • 9
  • Which version of S2? Some of the form code has changed. – Cerad Feb 25 '14 at 17:13
  • 1
    It seems that we are using 2.4 but was migrated from 2.1 (I'm new on this project). After testing some more functions and seeing that for example we are missing form_start(form) from twig module. I will install a new fresh 2.4 version and move our bundles there since it may happen that something broke on the migration. – Daerum Feb 26 '14 at 08:53
  • possible duplicate of [The CSRF token is invalid. Please try to resubmit the form](http://stackoverflow.com/questions/23455780/the-csrf-token-is-invalid-please-try-to-resubmit-the-form) – M Khalid Junaid Jul 28 '15 at 20:08
  • After starting a fresh 2.4 installation everything worked fine. It makes sense that using { form._token } would fix the problem. I'm not sure if call worked in 2.1. Anyway that code was wrong, as @Cerad stated, it had calls from a different version of what we had installed. In this case that did the trick. – Daerum Jul 30 '15 at 07:29
  • @M Khalid Junaid - Technically this would be a pre-duplicate since this was asked several months before the one you referenced. I guess it would be interesting to try to predict which questions will end up being duplicated and flagging them accordingly. – Cerad Jul 30 '15 at 14:43

0 Answers0