Bonjour,

j'ai cette erreur quand il submit formulaire : 'This form should not contain extra fields'

code controller:

<?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(),));
        }

}

cod service:

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

            return $form;
        }

        /**
         *function persisteAndFlush
         */

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

code SkillType:

/**
         * {@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_protection" => "false"
            ));
        }

code vue:

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

    {{ form_start(form) }}

        {{ form_row(form.nomComp) }}

    <input type="submit" value="Create" />
        {{ form_end(form) }}

    {%endblock%}

comment résoudre cetter probléme s'il vous plait et merci d'avance

5 réponses


Balbert
Réponse acceptée

Hello

ajoute => 'allow_extra_fields' => true a ton formulaire

/**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Skill',
                "csrf_protection" => "false",
                 "allow_extra_fields" => true
            ));
        }

Salut,

Je ne suis pas sure à 100% mais il doit manquer des fields, essaye de cette façon.

        {{ form_start(form) }}
        {{ form_widget(form) }}
        <button class="" type="submit">Create</button>
        {{ form_end(form) }}

    {%endblock%}
med001
Auteur

@nova ,
je modife mon code et j'ajoute ton code mais même probléme :(

Essaye un var_dump($form->getExtraData()); et affiche le var_dump car je ne comprends pas d'où vient l'erreur ...

med001
Auteur

getExtraData contient contenu du chap nomComp