title: How can I check whether the supplied CSRF token is invalid in Symfony2?
body: <p>I have created a Symfony2 form and bound it to the Request. I need to explicitly ensure whether the CSRF token is valid/invalid before proceeding with the rest of the form.</p>
  
  <p><code>$form['_token']-&gt;isValid()</code> throws <code>OutOfBoundsException</code> with message "Child _token does not exist."</p>
  
  <p>I can still verify that the rendered form contains <code>_token</code> field. In case that CSRF value is invalid, <code>$form-&gt;isValid()</code> returns false.</p>
  
  <p>What am I missing here?</p>
  
  <hr>
  
  <p>Update 1: </p>
  
  <p>Controller (partial):</p>
  
  <pre><code>private function buildTestForm() {
      $form = $this-&gt;createFormBuilder()
              -&gt;add('name','text')
              -&gt;getForm();
      return $form;
  }
  
  /**
   * @Route("/test/show_form", name="test.form.show")
   * @Method("GET")
   */
  public function showFormTest()
  {
      $form = $this-&gt;buildTestForm();
      return $this-&gt;render('TestBundle::form_test.html.twig', array('form' =&gt; $form-&gt;createView()));
  }
  
  /**
   * @Route("/test/submit_form", name="test.form.submit")
   * @Method("POST")
   */
  public function formTest()
  {
      $form = $this-&gt;buildTestForm();
      $form-&gt;bind($this-&gt;getRequest());
      if ($form['_token']-&gt;isValid()) {
          return new Response('_token is valid');
      } else {
          return new Response('_token is invalid');
      }
  }
  </code></pre>
  
  <p>Template</p>
  
  <pre><code>{# Twig template #}
  &lt;form action="{{ path('test.form.submit') }}" method="post" {{ form_enctype(form) }}&gt;
      {{ form_widget(form) }}
      &lt;input type="submit" name="go" value="Test Form" /&gt;
  &lt;/form&gt;
  </code></pre>