Creating and Displaying Comments

In this section, we implement the comment display and creation features.

In order to enhance the user interactivity, we would like to prompt users the possible errors each time he finishes entering one field. This is known client-side input validation. We will show how this can be done in Yii seamlessly and extremely easy. Note that this requires Yii version 1.1.1 or later.

Displaying Comments

Instead of displaying and creating comments on individual pages, we use the post detail page (generated by the view action of PostController). Below the post content display, we display first a list of comments belonging to that post and then a comment creation form.

In order to display comments on the post detail page, we modify the view script /wwwroot/blog/protected/views/post/view.php as follows,



In the above, we call renderPartial() to render a partial view named _comments to display the list of comments belonging to the current post. Note that in the view we use the expression $model->comments to retrieve the comments for the post. This is valid because we have declared a comments relation in the Post class. Evaluating this expression would trigger an implicit JOIN database query to bring back the proper comments. This feature is known as lazy relational query.

The partial view _comments is not very interesting. It mainly goes through every comment and displays the detail of it. Interested readers may refer to /wwwroot/yii/demos/blog/protected/views/post/_comments.php.

Creating Comments

To handle comment creation, we first modify the actionView() method of PostController as follows,



And then we modify the Post model class by adding the method addComment() as follows,



In the above, we call the newComment() method before we render view. In the newComment() method, we generate a Comment instance and check if the comment form is submitted. If so, we try to add the comment for the post by calling $post->addComment($comment). If it goes through, we refresh the post detail page, which will display the newly created comment unless approval is required. In the case where the comment first requires approval prior to display, we will show a flash message to indicate to the user that the comment will be displayed once approved. A flash message is usually a confirmation message displayed to end users. If the user clicks on the refresh button of his browser, the message will disappear.

We also need to modify /wwwroot/blog/protected/views/post/view.php furthermore,



In the above code, we display the flash message if it is available. If not, we display the comment input form by rendering the partial view /wwwroot/blog/protected/views/comment/_form.php.

Ajax-based Validation

In order to improve user experience, we can use Ajax-based form field validation so that the user is provided with validation feedback as they fill out the form, before having to submit the entire form to the server. To support Ajax-based validation on the comment form, we need to make some minor changes to both the comment form view /wwwroot/blog/protected/views/comment/_form.php and the newComment() method.

In the _form.php file, we mainly need to set CActiveForm::enableAjaxValidation to be true when we create the CActiveForm widget:



And in the newComment() method, we insert a piece of code to respond to the AJAX validation requests. The code checks if there is a POST variable named ajax. If so, it displays the validation results by calling CActiveForm::validate.



$Id$