Bonjour,
J'aimerais tous simplement mettre des commentaires a mes articles.
Probleme, je n'arrive pas à configurer correctement cela.
Ce que je n'arrive pas a comprendre c'est comment récupéré l'id et la catégorie de la news pour les envoyer dans les champs hidden du formulaire ?
Voici deja mes fichiers :
BaseCommentForm
Voici ensuite la version que j'ai faite en configuration :
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 class BaseCommentForm extends BaseFormDoctrine { public function setup() { $this->setWidgets(array( 'id' => new sfWidgetFormInputHidden(), 'article_id' => new sfWidgetFormDoctrineSelect(array('model' => 'Article', 'add_empty' => false)), 'content' => new sfWidgetFormTextarea(), 'author_id' => new sfWidgetFormDoctrineSelect(array('model' => 'Author', 'add_empty' => false)), 'category_id' => new sfWidgetFormDoctrineSelect(array('model' => 'Category', 'add_empty' => false)), 'email' => new sfWidgetFormInput(), 'created_at' => new sfWidgetFormDateTime(), 'updated_at' => new sfWidgetFormDateTime(), 'slug' => new sfWidgetFormInput(), )); $this->setValidators(array( 'id' => new sfValidatorDoctrineChoice(array('model' => 'Comment', 'column' => 'id', 'required' => false)), 'article_id' => new sfValidatorDoctrineChoice(array('model' => 'Article')), 'content' => new sfValidatorString(), 'author_id' => new sfValidatorDoctrineChoice(array('model' => 'Author')), 'category_id' => new sfValidatorDoctrineChoice(array('model' => 'Category')), 'email' => new sfValidatorString(array('max_length' => 255, 'required' => false)), 'created_at' => new sfValidatorDateTime(array('required' => false)), 'updated_at' => new sfValidatorDateTime(array('required' => false)), 'slug' => new sfValidatorString(array('max_length' => 255, 'required' => false)), )); $this->widgetSchema->setNameFormat('comment[%s]'); $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); parent::setup(); } public function getModelName() { return 'Comment'; }
Mon fichier action.class :
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 CommentForm extends BaseCommentForm { public function configure() { unset($this['id'], $this['created_at'], $this['updated_at']); $this->setWidgets(array( 'article_id' => new sfWidgetFormInputHidden(), 'category_id' => new sfWidgetFormInputHidden(), 'author_id' => new sfWidgetFormInputHidden(), 'email' => new sfWidgetFormInput(), 'content' => new sfWidgetFormTextarea(), )); $this->widgetSchema->setNameFormat('comment[%s]'); $this->setValidators(array( 'article_id' => new sfValidatorString(array('required' => true)), 'category_id' => new sfValidatorString(array('required' => true)), 'author_id' => new sfValidatorDoctrineChoice(array('model' => 'author', 'column' => 'id', 'required' => true)), 'email' => new sfValidatorEmail(array(),array('invalid' => 'Le format de l\'adresse email n\'est pas valide, veuillez recommencer.')), 'content' => new sfValidatorString(array('min_length' => 4), array( 'required' => 'Vous avez oublié le message.', 'min_length' => 'Le message "%value%" doit contenir au moins %min_length% caractères.' )))); } }
Et pour finir l'affichage :
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 public function executeShow(sfWebRequest $request) { $this->article = Doctrine::getTable('Article')->find($request->getParameter('id')); $this->forward404Unless($this->article); $this->comment_list = Doctrine::getTable('Comment')->getComment($com = $request->getParameter('id')); $this->comment_form = new BaseCommentForm(); if ($this->comment_form->isValid()) { $this->comment_form->save(); $params= array( 'comment_email' => $request->getParameter('comment[email]'), 'comment_content' => $request->getParameter('comment[content]'), 'comment_author_id' => $request->getParameter('comment[author_id]'), 'comment_article_id' => $request->getParameter('comment[article_id]'), 'comment_category_id' => $request->getParameter('comment[category_id]'), ); $this->redirect('article/thankyou?'.http_build_query($params)); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <!-- Affichage du formulaire de commentaire --> <form action="<?php echo url_for('@default?module=article&action=show&id='.$article->getId()) ?>" method="POST"> <?php echo $comment_form ?> </form>
Partager