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 :

[SF2] Modal et Formulaire


Sujet :

Symfony PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2011
    Messages : 10
    Points : 14
    Points
    14
    Par défaut [SF2] Modal et Formulaire
    Bonjour,

    J'aurais besoin de votre aide. Je précise auparavant que j'ai fait le tour des forums mais aucune solution ne fonctionne..
    J'ai une entité TableMark composé d'une clé composite et de deux champs. Je dois via un formulaire ajouter un utilisateur à un objet TableMark. J'aimerai donc afficher ce formulaire dans un modal (popup) qui affichera la liste d'utilisateur. Cependant j'ai une erreur : CRSF token is invalid.

    Entité TableMark :
    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
     
    class TableMark {
     
        /**
         * @ORM\ManyToOne(targetEntity="Basket\LibrairyBundle\Entity\User", inversedBy="tableUsers")
         * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
         * */
        private $user;
     
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Basket\LibrairyBundle\Entity\Game", inversedBy="tableGames")
         * @ORM\JoinColumn(name="game_id", referencedColumnName="id")
         * */
        private $game;
     
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Basket\LibrairyBundle\Entity\RoleOTM", inversedBy="tableRoleOTMs")
         * @ORM\JoinColumn(name="roleOTM_id", referencedColumnName="id")
         * */
        private $roleOTM;
     
        /**
         * @ORM\ManyToOne(targetEntity="Basket\LibrairyBundle\Entity\Outsider", inversedBy="tableOutsiders")
         * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
         * */
        private $outsider;
    }
    Vue de la liste des games :
    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
     
    {% block basketadmin_body %}
            <link rel="stylesheet" href="{{ asset('bundles/basketlibrairy/plugins/select2/select2.min.css') }}">
            <script type="text/javascript" charset="utf8" src="{{ asset('bundles/basketlibrairy/plugins/jQuery/jQuery-2.1.4.min.js') }}"></script>
        <script src="{{ asset('bundles/basketlibrairy/plugins/select2/select2.full.min.js') }}"></script>
     
        <div id="test">
                    <table class="table table-bordered table-color">
                            <tr>
                            <th>Jour</th>
                            <th>Heure</th>
                            <th>Poste</th>
                            <th>Personne</th>
                            <th>Action</th>
                            </tr>
                            {% for game in listgamedays %}
                                    {% for tableGame in game.tableGames %}
                                            {% if tableGame.roleOTM.name == 'chronometreur' %}
                                            <tr>
                                                    <td rowspan="2">{{ game.dateGame|date('d-m-Y') }}</td>
                                                    <td rowspan="2">{{ game.hourBegin|date('H:i:s') }}</td>
                                                    <td>{{ tableGame.roleOTM.name }}</td>
                                                    {% if tableGame.user %}
                                                    <td> {{ tableGame.user.username }} </td>
                                            {% elseif tableGame.outsider %}
                                                    <td> {{ tableGame.outsider.name }} </td>
                                            {% else %}
                                                    <td>Personne</td>
                                            {% endif %}
                                            {% if tableGame.user or tableGame.outsider %}
                                                    <td>
                                                            <a href="{{path('basket_admin_choose_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-info btn--sqxs btn-load-modal" title="Modifier">
                                                        <i class="fa fa-pencil"></i>
                                                    </a>
                                        <a href="{{path('basket_table_remove_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-sq-xs btn-danger" data-toggle="tooltip" title="Supprimer">
                                            <i class="fa fa-times"></i>
                                        </a>
                                                    </td>
                                            {% else %}
                                                    <td>
                                        <a href="{{path('basket_admin_choose_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-info btn-sq-xs btn-load-modal" title="Ajouter">
                                                        <i class="fa fa-plus"></i>
                                                    </a>
                                                    </td>
                                            {% endif %}
                                            </tr>
                                            {% else %}
                                            <tr>
                                                    <td>{{ tableGame.roleOTM.name }}</td>
                                                    {% if tableGame.user %}
                                                    <td> {{ tableGame.user.username }} </td>
                                            {% elseif tableGame.outsider %}
                                                    <td> {{ tableGame.outsider.name }} </td>
                                            {% else %}
                                                    <td>Personne</td>
                                            {% endif %}
                                                    {% if tableGame.user or tableGame.outsider %}
                                                    <td>
                                                            <a href="{{path('basket_admin_choose_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-info btn-sq-xs btn-load-modal" title="Modifier">
                                                        <i class="fa fa-pencil"></i>
                                                    </a>
                                        <a href="{{path('basket_table_remove_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-sq-xs btn-danger" data-toggle="tooltip" title="Supprimer">
                                            <i class="fa fa-times"></i>
                                        </a>
                                                    </td>
                                            {% else %}
                                                    <td>
                                        <a href="{{path('basket_admin_choose_user_table', { 'id_game': game.id, 'poste_name': tableGame.roleOTM.name, 'id_day': day.id }) }}" class="btn btn-info btn-sq-xs btn-load-modal" title="Ajouter">
                                                        <i class="fa fa-plus"></i>
                                                    </a>
                                                    </td>
                                            {% endif %}
                                            </tr>
                                            {% endif %}
                                    {% endfor %}
                            {% endfor %}
                    </table>
            </div>
            <style>
                    .table-color {
                            border: 1px solid #000000;
                    }
            </style>
     
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-body">
     
                    </div>
                </div>
            </div>
        </div><br><br>
        <script>
            $("#test").on('click', '.btn-load-modal', function (e) {
                $.ajax({
                    type: "POST",
                    url: $(this).attr("href"),
                    success: function (data) {
                        $('.modal-body').html(data);
                    },
                    complete: function () {
                    },
                    beforeSend: function () {
                        $('#myModal').modal('toggle');
                        $('.modal-body').html("<div class='progress' id='loadingDiv'><div class='progress-bar progress-bar-striped progress-bar-success active' role='progressbar' \n\
                                            aria-valuenow='100' aria-valuemin='0' aria-valuemax='100' style='width: 100%'></div></div>");
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                                    alert(xhr.status);
                                    alert(thrownError);
                                }
                });
                return false;
            });
        </script>
    {% endblock %}
    Vue avec le formulaire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <form method="post" action="{{ path('basket_admin_choose_user_table', { 'id_game': id_game, 'poste_name': poste, 'id_day': id_day }) }}" {{ form_enctype(form) }}>
     
        {{ form_widget(form) }}
     
        <input type="submit" class="btn btn-primary"/>
     
    </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
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
     
        public function chooseUserTableAction(Request $request, $id_game, $poste_name, $id_day) {
            $em = $this->getDoctrine()->getManager();
     
            $tableMark = new TableMark();
            $form = $this->createForm(new TableMarkType(), $tableMark);
     
            $request = $this->getRequest();
            if ($request->getMethod() == 'POST') {
                $form->bind($request);
                if ($form->isValid()) {
                    return $this->redirect($this->generateUrl('basket_admin_table_list_gameday', array('id' => $id_day)));
                }
            }
     
            return $this->render('BasketAdminBundle:Table:addUserTable.html.twig', array(
                        'id_game' => $id_game,
                        'poste' => $poste_name,
                        'id_day' => $id_day,
                        'form' => $form->createView()
            ));
        }
     
        public function addUserTableAction(Request $request, $id_game, $poste_name, $id_day) { -> action non terminée
            $em = $this->getDoctrine()->getManager();
     
            $poste = $em
                ->getRepository('BasketLibrairyBundle:RoleOTM')
                ->findOneBy(array('name' => $poste_name));
            $table = $em
                    ->getRepository('BasketLibrairyBundle:TableMark')
                    ->findOneBy(array('game' => $id_game, 'roleOTM' => $poste->getId()));
     
            if ($table->getUser() === null && $table->getOutsider() === null) {
                $user = $request->request->get('usertable');
     
                $tableMark = $em
                    ->getRepository('BasketLibrairyBundle:TableMark')
                    ->setUserTable($user->getId(), $id_game, $poste->getId());
                ;
                $tableMark->execute();
            }/* elseif ($table->getUsername != ) {
     
            }*/
     
            return $this->redirect($this->generateUrl('basket_admin_table_list_gameday', array('id' => $id_day)));
        }
    Merci d'avance pour votre aide..

  2. #2
    Membre expert
    Avatar de dukoid
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2012
    Messages
    2 100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2012
    Messages : 2 100
    Points : 3 004
    Points
    3 004
    Par défaut
    ne pas oublier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    {# Token CSRF #}
    {{ form_widget(form._token) }}

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2011
    Messages : 10
    Points : 14
    Points
    14
    Par défaut
    Hello Dukoid,

    J'avais effectivement oublié le token. Mais cela ne résout pas le problème et pas d'erreurs dans le log..
    The CSRF token is invalid. Please try to resubmit the form.

  4. #4
    Membre expert
    Avatar de dukoid
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2012
    Messages
    2 100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2012
    Messages : 2 100
    Points : 3 004
    Points
    3 004
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     public function chooseUserTableAction(Request $request, $id_game, $poste_name, $id_day) {
    dump($request->request->all()); exit;
    
    
            $em = $this->getDoctrine()->getManager();
     
            $tableMark = new TableMark();
            $form = $this->createForm(new TableMarkType(), $tableMark);
     
            $request = $this->getRequest();
            if ($request->getMethod() == 'POST') {
                $form->bind($request);

    1. tu redefinis $request alors qu'il existe déjà !
    2. je ne sais pas quel version de symfony tu utilises mais ce n'est plus bind mais handleRequest qu'il faut utiliser.
    3. regarde ce que donne le dump, si le code crsf est dans la requete

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2011
    Messages : 10
    Points : 14
    Points
    14
    Par défaut
    Niquel ça fonctionne. C'était bien le redéfinition du request qui planté..

    Merci à toi

  6. #6
    Membre éprouvé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2013
    Messages
    739
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Novembre 2013
    Messages : 739
    Points : 1 022
    Points
    1 022
    Par défaut
    Essaie avec ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     $("#test").on('click', '.btn-load-modal', function (e) {
    	    e.preventDefault();
                $.ajax({

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [VBA] Transfert de données à un formulaire modal
    Par CinePhil dans le forum Access
    Réponses: 2
    Dernier message: 23/01/2008, 16h59
  2. Réponses: 10
    Dernier message: 05/02/2007, 17h07
  3. Formulaire modal non bloquant
    Par the big ben 5 dans le forum Langage
    Réponses: 7
    Dernier message: 01/02/2006, 17h58
  4. Formulaire modal et état
    Par pht33 dans le forum Access
    Réponses: 6
    Dernier message: 25/10/2005, 14h24
  5. Formulaire dans une fenêtre modale
    Par Amnesiak dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 28/02/2005, 14h25

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