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 :

Héritage et ORM : CSRF is invalid


Sujet :

Symfony PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 11
    Par défaut Héritage et ORM : CSRF is invalid
    Bonjour, j'ai un problème lorsque je veux mettre en oeuvre mon héritage sous Symfony.

    J'ai 2 sous-classes : Location et Vente qui héritent de la super-classe Bien.

    Avec doctrine j'ai réalisé ceci :

    CLASSE BIEN
    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
     
    <?php
     
    namespace CL\LesGenetsBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use CL\LesGenetsBundle\Entity\Vente;
    use CL\LesGenetsBundle\Entity\Location;
    use Symfony\Component\Validator\Constraints as Assert;
     
    /**
     * CL\LesGenetsBundle\Entity\Bien
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\BienRepository")
     * @ORM\InheritanceType("SINGLE_TABLE")
     * @ORM\DiscriminatorMap({"Vente" = "Vente", "Location" = "Location"})
     */
    class Bien
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @Assert\NotBlank()
         */
        private $id;
     
        /**
         * @var string $photo
         *
         * @ORM\Column(name="photo", type="string", length=128, nullable=false)
         * @Assert\NotBlank()
         */
        private $photo;
     
        /**
         * @var float $diagDPEA
         *
         * @ORM\Column(name="diagDPEA", type="float", length=2048, nullable=false)
         * @Assert\NotBlank()
         */
        private $diagDPEA;
     
        /**
         * @var float $diagDPEB
         *
         * @ORM\Column(name="diagDPEB", type="float", length=2048, nullable=false)
         * @Assert\NotBlank()
         */
        private $diagDPEB;
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set photo
         *
         * @param string $photo
         */
        public function setPhoto($photo)
        {
            $this->photo = $photo;
        }
     
        /**
         * Get photo
         *
         * @return string 
         */
        public function getPhoto()
        {
            return $this->photo;
        }
     
        /**
         * Set diagDPEA
         *
         * @param float $diagDPEA
         */
        public function setDiagDPEA($diagDPEA)
        {
            $this->diagDPEA = $diagDPEA;
        }
     
        /**
         * Get diagDPEA
         *
         * @return float 
         */
        public function getDiagDPEA()
        {
            return $this->diagDPEA;
        }
     
        /**
         * Set diagDPEB
         *
         * @param float $diagDPEB
         */
        public function setDiagDPEB($diagDPEB)
        {
            $this->diagDPEB = $diagDPEB;
        }
     
        /**
         * Get diagDPEB
         *
         * @return float 
         */
        public function getDiagDPEB()
        {
            return $this->diagDPEB;
        }
    }
    CLASSE VENTE
    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
     
    <?php
     
    namespace CL\LesGenetsBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use CL\LesGenetsBundle\Entity\Bien;
    use Symfony\Component\Validator\Constraints as Assert;
     
    /**
     * CL\LesGenetsBundle\Entity\Vente
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\VenteRepository")
     */
    class Vente extends Bien
    {
        /**
         * @var float $prixAppart
         *
         * @ORM\Column(name="prixAppart", type="float", length=2048, nullable=false)
         */
        private $prixAppart;
     
        /**
         * @var float $prixCave
         *
         * @ORM\Column(name="prixCave", type="float", length=2048, nullable=false)
         */
        private $prixCave;
     
        /**
         * @var float $prixGarage
         *
         * @ORM\Column(name="prixGarage", type="float", length=2048, nullable=false)
         */
        private $prixGarage;
     
        /**
         * @var float $prixParking
         *
         * @ORM\Column(name="prixParking", type="float", length=2048, nullable=false)
         */
        private $prixParking;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set prixAppart
         *
         * @param float $prixAppart
         */
        public function setPrixAppart($prixAppart)
        {
            $this->prixAppart = $prixAppart;
        }
     
        /**
         * Get prixAppart
         *
         * @return float 
         */
        public function getPrixAppart()
        {
            return $this->prixAppart;
        }
     
        /**
         * Set prixCave
         *
         * @param float $prixCave
         */
        public function setPrixCave($prixCave)
        {
            $this->prixCave = $prixCave;
        }
     
        /**
         * Get prixCave
         *
         * @return float 
         */
        public function getPrixCave()
        {
            return $this->prixCave;
        }
     
        /**
         * Set prixGarage
         *
         * @param float $prixGarage
         */
        public function setPrixGarage($prixGarage)
        {
            $this->prixGarage = $prixGarage;
        }
     
        /**
         * Get prixGarage
         *
         * @return float 
         */
        public function getPrixGarage()
        {
            return $this->prixGarage;
        }
     
        /**
         * Set prixParking
         *
         * @param float $prixParking
         */
        public function setPrixParking($prixParking)
        {
            $this->prixParking = $prixParking;
        }
     
        /**
         * Get prixParking
         *
         * @return float 
         */
        public function getPrixParking()
        {
            return $this->prixParking;
        }
    }
    CLASSE LOCATION
    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
     
    <?php
     
    namespace CL\LesGenetsBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use CL\LesGenetsBundle\Entity\Bien;
     
    /**
     * CL\LesGenetsBundle\Entity\Location
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\LocationRepository")
     */
    class Location extends Bien
    {
     
        /**
         * @var float $loyerAppart
         *
         * @ORM\Column(name="loyerAppart", type="float", length=2048, nullable=false)
         */
        private $loyerAppart;
     
        /**
         * @var float $loyerGarage
         *
         * @ORM\Column(name="loyerGarage", type="float", length=2048, nullable=false)
         */
        private $loyerGarage;
     
        /**
         * @var float $loyerParking
         *
         * @ORM\Column(name="loyerParking", type="float", length=2048, nullable=false)
         */
        private $loyerParking;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set loyerAppart
         *
         * @param float $loyerAppart
         */
        public function setLoyerAppart($loyerAppart)
        {
            $this->loyerAppart = $loyerAppart;
        }
     
        /**
         * Get loyerAppart
         *
         * @return float 
         */
        public function getLoyerAppart()
        {
            return $this->loyerAppart;
        }
     
        /**
         * Set loyerGarage
         *
         * @param float $loyerGarage
         */
        public function setLoyerGarage($loyerGarage)
        {
            $this->loyerGarage = $loyerGarage;
        }
     
        /**
         * Get loyerGarage
         *
         * @return float 
         */
        public function getLoyerGarage()
        {
            return $this->loyerGarage;
        }
     
        /**
         * Set loyerParking
         *
         * @param float $loyerParking
         */
        public function setLoyerParking($loyerParking)
        {
            $this->loyerParking = $loyerParking;
        }
     
        /**
         * Get loyerParking
         *
         * @return float 
         */
        public function getLoyerParking()
        {
            return $this->loyerParking;
        }
    }
    Maintenant, le problème : (Le formulaire, la vue et le controller existent)
    Je veux ajouter une vente et lors de la validation, j'ai l'erreur the CRSF token is invalid... Après des recherches sur internet j'ai pu constater que ce problème pouvait survenir lors que l'on essaye de submit plus d'un formulaire à la fois.

    Or je submit Location qui extend Bien, est-ce que vous pensez que c'est à cause de ça que j'obtiens ce message d'erreur ?

    FORM
    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
     
    <?php
    namespace CL\LesGenetsBundle\Form\Type;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilder;
    use CL\LesGenetsBundle\Entity\Vente;
     
    class VenteType extends AbstractType
    { 
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder
                ->add('photo', 'text')
                ->add('diagDPEA', 'text')
                ->add('diagDPEB', 'text')
                ->add('prixAppart', 'text')
                ->add('prixCave', 'text')
                ->add('prixGarage', 'text')
                ->add('prixParking', 'text')
            ;
        }
     
        public function getName()
        {
            return 'CL_LesGenetsBundle_VenteType';
        }
    VUE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    {{ form_errors(form) }}
    <form action="{{ path('ajouterVente') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    {{ form_rest(form) }}
    </form>
    CONTROLLER
    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
     
    public function ajouterVenteAction()
        {
            $vente = new Vente();
            $request = $this->getRequest();
            $form = $this->createForm(new VenteType(), $vente);
            if ($request->getMethod() == 'POST') {
                $form->bindRequest($request);            
                if ($form->isValid()) {
                    $em = $this->getDoctrine()->getEntityManager();
                    $em->persist($vente);
                    $em->flush();
                    $form = $this->createForm(new VenteType(), new Vente());
                    return $this->redirect($this->generateUrl('ajoutEffectue', array()));
                }
            }
            return $this->render(
                'CLLesGenetsBundle:Vente:ajouterVente.html.twig',
                array(
                    'form' => $form->createView()
                )
            );
        }
    Merci.

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

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

    je ne pense pas qu'il y ait aurait un lien entre les deux problèmes.

    pour commencer consultes la source html généré par le formulaire et regarde si tu vois un champ "_token"

    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
     
    public function ajouterVenteAction()
        {
            $vente = new Vente();
            $request = $this->getRequest();
            $form = $this->createForm(new VenteType(), $vente);
            if ($request->getMethod() == 'POST') {
                $form->bindRequest($request);            
                if ($form->isValid()) {
                    $em = $this->getDoctrine()->getEntityManager();
                    $em->persist($vente);
                    $em->flush();
                    $form = $this->createForm(new VenteType(), new Vente());//Pourquoi recréer un formulaire?, d'autant plus qu'il n'a pas l'air utilisé!!
                    return $this->redirect($this->generateUrl('ajoutEffectue', array()));
                }
            }
            return $this->render(
                'CLLesGenetsBundle:Vente:ajouterVente.html.twig',
                array(
                    'form' => $form->createView()
                )
            );
        }

  3. #3
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 11
    Par défaut
    Merci de ta réponse arnooo999 alors oui j'ai bien un champ _token dans mon code source généré.

    Concernant la ligne que tu as repéré, tu as raison de préciser qu'elle ne sert à rien en fait c'est juste que j'ai copier/coller le code d'une autre action ou cette ligne servait à préciser qu'en cas de réussite il fallait réinitialiser le formulaire. Je vais supprimer cette ligne.

  4. #4
    Membre Expert
    Avatar de Seb33300
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    1 564
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Thaïlande

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

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 564
    Par défaut
    Tu et sur de l'url dans le action ?
    {{ path('ajouterVente') }}

    Est ce que l'erreur PHP qui remonte correspond bien à la ligne if ($form->isValid()) { de la méthode ajouterVenteAction() de ton controller ?

  5. #5
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 11
    Par défaut
    Oui l'url de l'action est bonne et oui l'erreur survient à la ligne if ($form->isValid()) {

Discussions similaires

  1. [2.x] CSRF Token invalide
    Par siva_dashq dans le forum Symfony
    Réponses: 5
    Dernier message: 08/01/2015, 19h22
  2. [2.x] UPDATE 2.1 : Le jeton CSRF est invalide en mode DEV
    Par pmithrandir dans le forum Symfony
    Réponses: 6
    Dernier message: 03/12/2012, 19h55
  3. [2.x] "The CSRF token is invalid"
    Par keokaz dans le forum Symfony
    Réponses: 1
    Dernier message: 08/11/2012, 10h19
  4. Réponses: 5
    Dernier message: 11/10/2012, 16h01
  5. csrf token invalid
    Par blacksf dans le forum Ext JS / Sencha
    Réponses: 1
    Dernier message: 08/09/2012, 12h17

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