I’ve been following the blog tutorial and it is helping a lot to understand how Yii framework works.
Now I am at the Creating and displaying comments section of the tutorial and I can’t save comments.
In fact, I don’t get an error summary or any feedback at all if I miss required fields.
This my controller (it is named Robots instead of Post):
public function actionShow()
{
$robots = $this->loadrobots();
$robot_comment = $this->newComment($robots);
$this->render('show', array('model'=>$robots, 'robots_comments'=>$robots->robots_comments, 'newComment' => $robot_comment));
//$this->render('show',array('model'=>$this->loadrobots()));
}
protected function newComment($robots)
{
$robot_comment = new robots_comments;
if (isset($_POST['robots_comments']))
{
$robot_comment->attributes = $_POST['robots_comments'];
$robot_comment->robot = $robots->id;
$robot_comment->fecha = time();
$robot_comment->status = robots_comments::STATUS_PENDING;
if (isset($_POST['submitComment']) && $robot_comment->save())
{
Yii::app()->user->setFlash('commentSubmited','Thank you');
$this->refresh();
}
}
return $robot_comment;
}
This is the snippet that renders the comment form:
<?php $this->renderPartial('/robots_comments/_form',array(
'comment'=>$newComment,
'robots'=>$model,
'update'=>false,
)); ?>
This is the beginning of the comment form:
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="form">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
</div>
<?php return; endif; ?>
<div class="yiiForm">
<p>
Fields with <span class="required">*</span> are required.
</p>
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($comment); ?>
Any help will be greatly appreciated!