Hello,
Good news: This article is very helpful:)
It will allow us to treat a form on the fly and recover from errors that could send poor filling.
Suppose we want to add a comment to a blog.
We display our article and below, it displays a link to comment on the article.
The comment form must implicitly know about the comment.
Also, it will:
1. edit the comment form to enter the id of the article sweet (thanks to the widget sfWidgetFormInputHidden
/ lib / form / doctrine / CommentForm.php
public function configure()2. pass the id of the article when loading the form
{
$this->widgetSchema['model'] = new sfWidgetFormInputHidden();
$this->getWidgetSchema()->setLabels(array(
'author'=> "Pseudo :",
'Content' => "Message:"
));}
You can either display the form when the page loads, or Ajax, Ajax clients, it would be too easy;)
We will therefore use JQuery. Install the plugin already sfJqueryReloadedPlugin
Once that is done and that was good includes the jQuery helper, you can create a js file that will include one (I trust you there above) a function that is triggered when clicking on the button comment.
/ app / frontend / blog / show / myArticle
\u0026lt;div>\u0026lt;h1>\u0026lt;? Php echo $ article-> getTitle ();?>\u0026lt;/ h1>\u0026lt;p> \u0026lt;? php echo $ article-> getContent ( );?> \u0026lt;/ p></div><input type="button" value="Commenter" onclick="comment(<?php echo $article->getId(); ?>)"/>
<div id="commentForm"></div>
/web/js/main.js
Until then, it is not too confused, it's just the way to load the comment form.function comment(arg){ $.post("/comment/new", {id: arg}, function(data){$ ("# CommentForm). Html (data);$ (" # CommentForm). slideToggle ();});}
We must now establish how the action in the module Blog:
/ app / frontend / modules / comment / actions / actions.php
} You've probably realized in the preceding code, we created comment object to pre-fill with the id of the article. Then loaded commentSuccess.php. Create it as you want, I will personally go for much simpler to show the ease of the process.public function executeComment (sfWebRequest $ request) {$ comment= new Comment ();$ comment -> setArticleId ($ request-> getParameter ("id"));$ this-> form = new CommentForm ($ comment);
/ app / frontend / modules / comment / templates / newSuccess.php
réinclut the jQuery helper. Why? because otherwise, the view, isolated as responsible Ajax can not find the function jq_form_remote_tag. We spent 2\u0026lt;? Php use_helper ('jquery');>\u0026lt;? Php echo jq_form_remote_tag (array ('update' => 'CommentForm', / * represents the id of the div that we should update * /'url' => 'how /'.($ form-> getObject () -> isNew ()?' Create ':' update '),));?>\u0026lt;? php echo $ form;?>\u0026lt;input type="submit" value="Commenter"/>\u0026lt;/ form>It
parameter to this function : Url, which means the action that will do the update form and which defines what the page id of the ajax action will reveal the answer. Here in the div id CommentForm.
Now we need to say what we'll see where this will be validated. The form, once approved and registered raffiche form for editing, running by default but that does not suit us. We can to change that by adding a test in the template newSuccess.php. With the attribute
$ form-> getObject () -> isNew () forms, one can infer the behavior that we must adopt either display the form (behavior 1) either display the comment (behavior 2).
/ app / frontend / modules / comment / templates / newSuccess.php
\u0026lt;? Php if ($ form-> getObject () -> isNew ()){?>\u0026lt;h3> \u0026lt;? php echo __ ("Add a comment :");?> \u0026lt;/ h3>\u0026lt;? php include_partial ('form', array ('form' => $ form))?>\u0026lt;? php} else {include_partial ("show ", array ('how' => $ comment));}?>
That is fact. Now. The form is in Ajax, if there are errors in filling the form, then the errors will be displayed when this is valid, the new comment will be posted with the comments through _show.php file you have created by your desire for formatting comments.
0 comments:
Post a Comment