CSRF令牌无效。请尝试重新提交

时间:2017-11-01 20:34:42

标签: php symfony

我一直在尝试提交一个持久存在对象的表单。当我尝试这样做时,显示错误消息:

enter image description here

我使用服务来创建表单,并使用控制器来调用数据。 我添加{{ form_rest(form) }}{{ 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';
    }


}

代码树枝:

{% 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%}

如何解决此错误?

2 个答案:

答案 0 :(得分:0)

检查生成的html页面的源代码。我想,你在某个地方有一个额外的let str = NSString(string: text) let theRange = str.range(of: "BOLD TEXT") let attribute = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedStringKey.foregroundColor: UIColor.red] attributedString.addAttributes(attribute, range: theRange) 标签。

尝试删除行<form></form>
您将使用后者{{ form_widget(form.nomComp) }}呈现此表单字段。

答案 1 :(得分:0)

试试这个:

{% 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) }}