简体   繁体   中英

The CSRF token is invalid. Please try to resubmit

I've been trying to submit a form which persists an object. When I try to do it this error message is showing:

在此处输入图片说明

I use a service for creating the form and I use a controller for invoke data. I add {{ form_rest(form) }} and {{ form_row(form._token) }}

SkillController.php:

<?php 

namespace AppBundle\Controller\Condidate;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Skill;
use AppBundle\Controller\AbstractController;
use AppBundle\Form\SkillType;

/**
 *Class SkillController.
 */
class SkillController extends AbstractController
{

     /**
     *function handler.
     */
     protected function getHandler(){
        return $this->get('recurit.handler.skill');
    }

    public function getSkillAction(Request $request){
         return $this->render('skills/listkill.html.twig');
    }

    /**
     *function addSkill
     * @param Request $request
     * @return \Symfony\Component\Form\Form The form
     */
    public function addSkillAction(Request $request) {

          $skill =$this->getHandler()->post($request->request->all());
        if ($skill instanceof Skill) {
            //return $this->redirectToRoute('list_skills');
    }

    return $this->render('skills/newskill.html.twig', array(
        'form' => $skill->createView(),));
    }


    }

SkillHandler.php:

<?php 

namespace AppBundle\Handler;

use AppBundle\Handler\HandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Skill;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\formFactory;

/**
 * SkillHandler.
 */
class SkillHandler implements HandlerInterface {

    /**
     *
     * @var EntityManager 
     */
    protected $em;

    /**
     *
     * @var formFactory 
     */
    private $formFactory;


    /**
     *function construct.
     */
    public function __construct(EntityManager $entityManager, formFactory $formFactory)
        {
            $this->em = $entityManager;
            $this->formFactory = $formFactory;
        }

     /**
       *function post
       */
    public function post(array $parameters, array $options = []) {
        $form = $this->formFactory->create(\AppBundle\Form\SkillType::class, new Skill(), $options);
        $form->submit($parameters);
       var_dump($form->getErrors());
        if ($form->isValid()) {
            var_dump('1');
            $skill = $form->getData();
            $this->persistAndFlush($skill);
            return $skill;
        }

        return $form;
    }

    /**
     *function persisteAndFlush
     */

    protected function persistAndFlush($object) {
       $this->em->persist($object);
       $this->em->flush();
    }


    /**
     *function get
     */
     public function get($id){
        throw new \DomainException('Method SkillHandler::get not implemented');

     }

     /**
      *function all
      */
      public function all($limit = 10, $offset = 0){
        throw new \DomainException('Method SkillHandler::all not implemented');
      }


    /**
     *function put
     */
    public function put($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::put not implemented');
    }

    /**
     *function patch
     */
    public function patch($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::patch not implemented');
    }

    /**
     *function delete
     */
     public function delete($resource){
        throw new \DomainException('Method SkillHandler::delete not implemented');
         }
}

SkillType.php:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class SkillType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nomComp',TextType::class);
        $builder->add('submit',SubmitType::class);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Skill',
            'csrf_field_name' => '_token',

        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_skill';
    }


}

code twig:

{% extends 'base.html.twig' %}
{% block body %}

{{ form_start(form) }}

    {#{{ form_label(form.nomComp) }}
    {{ form_widget(form.nomComp) }}
    {{ form_errors(form.nomComp) }}
#}
    {{ form_row(form.nomComp) }}
    {{ form_row(form.submit) }}

    {{ form_row(form._token) }}



    {#<input type="submit" value="Create" name="form[submit]"/>#}
    {{ form_rest(form) }}

    {{ form_end(form) }}

{%endblock%}

How can I resolve this error?

Check the source code of the generated html page. I guess, you have an extra <form></form> tag somewhere.

Try to remove the line {{ form_widget(form.nomComp) }} .
You will render this form field with the latter {{ form_row(form.nomComp) }} .

Try this:

{% set formId = form.vars.id~'Form' %}
{{ form_start(form) }}
{{ form_errors(form) }}
            {{ form_widget(form.nomComp) }}
            {{ form_widget(form.submit) }}          
{{ form_end(form) }}
{{ form_rest(form) }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM