Blog tutorial: can't save comments

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!

if there is no any feedback,means


if (isset($_POST['submitComment']) && $robot_comment->save())

does not work,check on $_POST[‘submitComment’] or $robot_comment->save(),make sure which one is false.

if the save() false,check the beforeValidate() method of the model,it should always return true. or check the beforeSave() method.

i had the same problem,qiang give the answer.

u can try it.good luck!

Thank you, thank you, thank you!!

I had a beforeSave that didn’t return true and was giving me that headache!

Now, the questions left are:

1- Why I don’t get the error message if I miss a required field?

2- If it is successful, why I don’t get the flash message?