IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

[Form] This form should not contain extra fields


Sujet :

Symfony PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Avril 2011
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2011
    Messages : 139
    Points : 65
    Points
    65
    Par défaut [Form] This form should not contain extra fields
    Salut

    Je voudrai enregistrer un topic donc j'ai créé une entité topic et une autre nommée poste voila les deux fichiers :

    Topic.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    <?php
     
    namespace Siteweb\ForumBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Siteweb\ForumBundle\Entity\Topic
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Siteweb\ForumBundle\Entity\TopicRepository")
     */
    class Topic
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id; 
     
        /**
         * @ORM\OneToOne(targetEntity="Siteweb\ForumBundle\Entity\Poste")
         */
        private $poste;           
     
        /**
         * @ORM\ManyToOne(targetEntity="Siteweb\ForumBundle\Entity\Forum")
         */
        private $forum;
     
        /**
         * @var string $topicTitre
         *
         * @ORM\Column(name="topicTitre", type="string", length=255)
         */
        private $topicTitre;
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set topicTitre
         *
         * @param string $topicTitre
         */
        public function setTopicTitre($topicTitre)
        {
            $this->topicTitre = $topicTitre;
        }
     
        /**
         * Get topicTitre
         *
         * @return string 
         */
        public function getTopicTitre()
        {
            return $this->topicTitre;
        }
     
        /**
         * Set forum
         *
         * @param Siteweb\ForumBundle\Entity\Forum $forum
         */
        public function setForum(\Siteweb\ForumBundle\Entity\Forum $forum)
        {
            $this->forum = $forum;
        }
     
        /**
         * Get forum
         *
         * @return Siteweb\ForumBundle\Entity\Forum 
         */
        public function getForum()
        {
            return $this->forum;
        }    
     
        /**
         * Set poste
         *
         * @param Siteweb\ForumBundle\Entity\Poste $poste
         */
        public function setPoste(\Siteweb\ForumBundle\Entity\Poste $poste)
        {
            $this->poste = $poste;
        }
     
        /**
         * Get poste
         *
         * @return Siteweb\ForumBundle\Entity\Poste 
         */
        public function getPoste()
        {
            return $this->poste;
        }
    }
    Poste.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    <?php
     
    namespace Siteweb\ForumBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Siteweb\ForumBundle\Entity\Poste
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Siteweb\ForumBundle\Entity\PosteRepository")
     */
    class Poste
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @ORM\ManyToOne(targetEntity="Siteweb\ForumBundle\Entity\Topic")
         */
        private $topic;
     
        /**
         * @var text $posteTexte
         *
         * @ORM\Column(name="posteTexte", type="text")
         */
        private $posteTexte;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set posteTexte
         *
         * @param text $posteTexte
         */
        public function setPosteTexte($posteTexte)
        {
            $this->posteTexte = $posteTexte;
        }
     
        /**
         * Get posteTexte
         *
         * @return text 
         */
        public function getPosteTexte()
        {
            return $this->posteTexte;
        }
     
        /**
         * Set topic
         *
         * @param Siteweb\ForumBundle\Entity\Topic $topic
         */
        public function setTopic(\Siteweb\ForumBundle\Entity\Topic $topic)
        {
            $this->topic = $topic;
        }
     
        /**
         * Get topic
         *
         * @return Siteweb\ForumBundle\Entity\Topic 
         */
        public function getTopic()
        {
            return $this->topic;
        }
    }
    Puis j'ai créé un formulaire avec un fichier "handler" pour l'entité topic et deux fichiers "type" pour les deux entités topic et poste car je voudrai imbriquer le champ du formulaire de poste dans le formulaire de topic vu que le message du topic sera enregistré dans la table poste et le titre du topic dans la table topic

    Voici donc les fameux fichiers :

    TopicType.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <?php
     
    // src/Siteweb/ForumBundle/Form/TopicType.php
     
    namespace Siteweb\ForumBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    class TopicType extends AbstractType
    {
       public function buildForm(FormBuilder $builder, array $options)
       {
          $builder->add('topictitre',   'text')
                  ->add('', new PosteType());              
       }
     
       public function getName()
        {
            return 'siteweb_forumbundle_topictype';
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Siteweb\ForumBundle\Entity\Topic',
            );
        }
    }
    PosteType.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <?php
     
    // src/Siteweb/ForumBundle/Form/PosteType.php
     
    namespace Siteweb\ForumBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    class PosteType extends AbstractType
    {
       public function buildForm(FormBuilder $builder, array $options)
       {
          $builder->add('posteTexte',  'textarea');
       }
     
       public function getName()
        {
            return 'siteweb_forumbundle_postetype';
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Siteweb\ForumBundle\Entity\Poste',
            );
        }
    }
    TopicHandler.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    <?php
    // src/Siteweb/ForumBundle/Form/TopicHandler.php
     
    namespace Siteweb\ForumBundle\Form;
     
    use Symfony\Component\Form\Form;
    use Symfony\Component\HttpFoundation\Request;
    use Doctrine\ORM\EntityManager;
    use Site\ForumBundle\Entity\Topic;
    use Site\ForumBundle\Entity\Poste;
     
    use Site\ForumBundle\Entity\TopicRepository;
    class TopicHandler
    {
        protected $form;
        protected $request;
        protected $em;
     
        public function __construct(Form $form, Request $request, EntityManager $em)
        {
            $this->form    = $form;
            $this->request = $request;
            $this->em      = $em;
        }
     
        public function process()
        {
            if( $this->request->getMethod() == 'POST' )
            {
                $this->form->bindRequest($this->request);
     
                if( $this->form->isValid() )
                {
                    $this->onSuccess($this->form->getData());
     
                    return true;
                }
            }
     
            return false;
        }
     
        public function onSuccess(Topic $topic)
        {
            $this->em->persist($topic);
            $this->em->persist($topic->getPoste());
            $this->em->flush();
        }
    }

    Mon formulaire s'affiche tres bien le prebléme c'est que quand je veux enregistrer en appuyant sur le bouton submit et bien j'ai ce message qui s'affiche :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    This form should not contain extra fields
    Pouvez vous m'aider svp ?

    Cordialement

  2. #2
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Le soucis est déjà résolu : regarde ceci :

    http://www.developpez.net/forums/d11...mp-formulaire/

  3. #3
    Membre du Club
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Avril 2011
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2011
    Messages : 139
    Points : 65
    Points
    65
    Par défaut
    Citation Envoyé par aitiahcene Voir le message
    Le soucis est déjà résolu : regarde ceci :

    http://www.developpez.net/forums/d11...mp-formulaire/
    J'ai pas tres bien compris la solution

  4. #4
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    Bonjour,

    Au niveau de ton mapping je comprend pas bien que Topic ait une relation OneToOne vers Poste et Poste une relation ManyToOne vers Topic.
    S'il s'agit d'une relation bidirectionelle c'est un OneToMany / ManyToOne.

    Ici il s'agit il me semble de créer un nouveau Topic et de créer le premier Post de ce Topic.
    Si c'est le cas tu pourrais mapper ton formulaire sur une entité Poste
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    class PosteType extends AbstractType
    {
       //indique s'il faut également éditer le Topic lié au Post
       protected $initTopic;
     
      public function __construct($initTopic=false){
         $this->initTopic=$initTopic;
     }
      public function buildForm(FormBuilder $builder, array $options)
       {
          $builder->add('posteTexte',  'textarea');
          if($this->initTopic){
             $builder->add('topic',new TopicType());//TopicType ne contient que le champ titre du Topic
          }
       } 
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Siteweb\ForumBundle\Entity\Poste',
            );
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public function createTopicAction($idForum){
             $forum=$this->getDoctrine()->getEntityManager()->getRepository('Forum')->find($idForum);
            $topic=new Topic();
            $topic->setForum($forum);
            $firstPost=new Poste();
            $firstPost->setTopic($topic);
            $topic->addPost($post);
     
            $formPost=$this->createForm(new TopicType(true),$firstPost);
            //etc.. procéder au traitement du formulaire si méthod post par ex
    }
    ou bien en reprenant ton code, ajouter un champ Collection de PostType en préremplissant les poste de Topic avec une entité Post

  5. #5
    Membre du Club
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Avril 2011
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2011
    Messages : 139
    Points : 65
    Points
    65
    Par défaut
    Salut arnooo

    Voila ce que j'ai fais et malheureusement j'ai toujours l'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    This form should not contain extra fields


    voila mes bouts de code

    la fonction qui crée un nouveau topic

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public function ajouterTopicAction($idForum)
        {
           $id = $idForum;
           $forum = $this->getDoctrine()
                               ->getEntityManager()
                               ->getRepository('SitewebForumBundle:Forum')
                               ->find($id);
     
           $poste = new Poste();  
     
           $formPoste = $this->createForm(new PosteType, $poste);        
     
           $formHandlerPoste = new PosteHandler($formPoste, $this->get('request'), $this->getDoctrine()->getEntityManager(), $forum);
     
     
     
           if( $formHandlerPoste->process())
           { 
              return $this->redirect( $this->generateUrl('SitewebForum_voirForum', array('id' => $id)) );
           }
     
     
     
     
           return $this->render('SitewebForumBundle:Forum:ajouterTopic.html.twig', array(
               'form' => $formPoste->createView()         
           ));
       }
    fichier PosteType.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <?php
     
    // src/Siteweb/ForumBundle/Form/PosteType.php
     
    namespace Siteweb\ForumBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
     
    use Siteweb\ForumBundle\Entity\Topic;
     
    class PosteType extends AbstractType
    {
     
     
       public function buildForm(FormBuilder $builder, array $options)
       {
     
            $builder->add('',     new TopicType)     
                    ->add('postetexte', 'textarea');
       }
     
       public function getName()
        {
            return 'siteweb_forumbundle_postetype';
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Siteweb\ForumBundle\Entity\Poste',
            );
        }
    }
    fichier PosteHandler.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    <?php
    // src/Siteweb/ForumBundle/Form/TopicHandler.php
     
    namespace Siteweb\ForumBundle\Form;
     
    use Symfony\Component\Form\Form;
    use Symfony\Component\HttpFoundation\Request;
    use Doctrine\ORM\EntityManager;
    use Siteweb\ForumBundle\Entity\Poste;
    use Siteweb\ForumBundle\Entity\Topic;
     
     
    class PosteHandler
    {
        protected $form;
        protected $request;
        protected $em;
     
     
        public function __construct(Form $form, Request $request, EntityManager $em, \Siteweb\ForumBundle\Entity\Forum $forum)
        {
            $this->form    = $form;
            $this->request = $request;
            $this->em      = $em; 
            $this->forum      = $forum;                 
        }
     
        public function process()
        {
            if( $this->request->getMethod() == 'POST')
            {
                $this->form->bindRequest($this->request);
     
                if( $this->form->isValid())
                {
                    $this->onSuccess($this->form->getData());
     
                    return true;
                }
            }
     
            return false;
        }
     
        public function onSuccess(Poste $poste)
        { 
            $topic = new Topic();
            $topic->setForum($this->forum);
            $this->em->persist($topic);
     
     
            $poste->setDateCreation(new \Datetime());
            $poste->setDateModification(new \Datetime());
            $this->em->persist($poste->getTopic());
            $this->em->persist($poste);       
            $this->em->flush();        
        }
    }
    Peux tu m'aider stp ?

    Cordialement

Discussions similaires

  1. Réponses: 2
    Dernier message: 08/10/2014, 08h39
  2. Réponses: 2
    Dernier message: 25/01/2012, 15h13
  3. this.form.submit ne marche pas ... et pourtant
    Par misarod dans le forum Balisage (X)HTML et validation W3C
    Réponses: 3
    Dernier message: 21/12/2005, 17h40
  4. onChange="this.form.submit();" marche pas sous IE
    Par Death83 dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 25/09/2005, 11h05
  5. pb de fonction simple (this.form.submit()..)
    Par petitsims dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 14/01/2005, 09h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo