Bonjour,
J'ai actuellement des commentaires et dans la boucle de ceux-ci j'ai un bouton delete sur chacun d'eux. Comme ce bon SF2 propose de créer un CSRF token pourquoi s'en priver ? Seulement je n'arrive pas à le répéter.
Voici mon controller :
et ma vue
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 public function viewAction(Post $post) { $comment = new Comment; $comment->setPost($post); $form = $this->createForm(new CommentType(), $comment); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $form->bind($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $this->get('session')->getFlashBag()->add('success', 'Le commentaire à bien été ajouté'); return $this->redirect($this->generateUrl('chan_view_post', array('slug' => $post->getSlug()))); } } $comments = $this->getDoctrine()->getManager()->getRepository('ChanChanBundle:Comment')->getCommentsByPost($post->getId()); return $this->render('ChanChanBundle:Default:view_post.html.twig', array( 'form' => $form->createView(), 'post' => $post, 'comments' => $comments )); } public function deleteCommentAction(Comment $comment){ $form = $this->createForm(new CommentType(), $comment); $request = $this->getRequest(); $form->bind($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->remove($comment); $em->flush(); $this->get('session')->getFlashBag()->add('success', 'Le commentaire à bien été supprimé'); } return $this->redirect($this->generateUrl('chan_view_post', array('slug' => $comment->getPost()->getSlug()))); }
Donc mon script fonctionne bien si j'enlève la validation des forms dans les deux actions, mais forcément j'en ai besoin.
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 {% extends "::base.html.twig" %} {% block title %}{{ post.name }} de {{ post.author }}{% endblock %} {% block description %}Un test de hotgeart{% endblock %} {% block keywords %}hotgeart, test, 123{% endblock %} {% block body %} <div class="row"> <div class="span8"> <div class="corgi_feed_well"> <div class="individual_feed_item"> <div class="feed_item"> <div class="feed_body"> <div class="fl"> <a href="#">{{ post.author }}</a> <span>|</span> <a href="#view_comments" class="show_comment_link">{{ post.ncomments }} comments</a> <span>|</span> {{ post.date|date('d/m/Y') }} </div> <div class="row clear"> <div class="feed_profile_pic"> <img src="{{ post.image }}" alt="meta image" class="meta_image"> </div> <div class="feed_text"> <p>{{ post.name }} <small>dans {{ post.category.name }}</small></p> </div> </div> <div class="btn-group pull-right"> <a href="{{ path('chan_delete_post', {'id': post.id }) }}" class="btn btn-small btn-danger"><i class="icon icon-trash"></i></a> <a href="{{ path('chan_edit_post', {'id': post.id }) }}" class="btn btn-small btn-success"><i class="icon icon-pencil"></i></a> </div> <div style="clear:both;"></div> </div> <hr class="feed_hr" /> <div class="bottom_meta"> <div class="row"> <div class="bottom_left"> <div class="share_wrapper"> </div> </div> <div class="bottom_left" style="float:none;"> <div class="padding"> <h5 id="view_comments">{{ post.ncomments }} Commentaires :</h5> </div> {% for comment in comments %} <div class="padding"> <p><strong><a title="{{ comment.date|date('d/m/Y') }}" href="#">{{ comment.author }}</a></strong> : {{ comment.text }}</p> <form action="{{ path('chan_delete_comment', {'id': comment.id}) }}" method="post"> <button class="btn btn-small pull-right btn-danger"><i class="icon icon-trash"></i></button> <div style="clear:both;"></div> {{ form_row(form._token) }} </form> </div> {% if loop.last == false %} <hr> {% endif %} {% else %} <div class="padding"> <p>Aucun commentaire, Soyez le premier à commenter !</p> </div> {% endfor %} <hr> <div class="padding"> <h5>Ajouter un commentaire :</h5> <form action="" method="post" {{ form_enctype(form) }}> {{ form_errors(form) }} <div> {{ form_label(form.author,'Autheur :') }} {{ form_widget(form.author) }} {{ form_errors(form.author) }} </div> <div> {{ form_label(form.text,'Commentaire :') }} {{ form_widget(form.text) }} {{ form_errors(form.text) }} </div> {{ form_rest(form) }} <button class="btn btn-primary">Ajouter</button> </form> </div> </div> </div> </div> </div> </div> </div> </div> {{ render(controller("ChanChanBundle:Category:list")) }} </div> {% endblock %}
Voici ce qui se passe actuellement :
- Je post un commentaire pas de soucis
- Je veux le supprimer directement pas de soucis
- Je post un commentaire, puis je veux en mettre un deuxième *CRACK* token invalide. L'input hidden token est passé sur l'input du delete de la boucle de l'affichage des comments. Donc tant que je ne delete pas le commentaire je ne peux pas en poster un deuxième.
Je vous avoue que je suis un peu paumé et je ne vois pas comment faire.
Merci
Partager