Check data before insert

Hi Experts,

I am creating sample application using yii. I have doubts on yii. Could you please help me.

  1. Before insert a new record into database,I need to check condition and return error to user. Suppose in the attached image, if user checks more than one default answer I need to check condition and return error to user.

Before save my code is

public function beforeSave()

{

if ($this->isNewRecord)

{

$this->create_time = new CDbExpression('NOW()');


$this->create_user_id = Yii::app()->user->id;

}

else

{

  	$this->update_time = new CDbExpression('NOW()');


	$this->update_user_id = Yii::app()->user->id;

}

return parent::beforeSave();

}

  1. In the order column I need display like {number) {up arrow image} {down arrow image}. How to include up & down arrow images.

Thanks & Regards,

Gangadhar

1245

insert.JPG

Hi there,

Have you tried using ‘on’ property for CValidator? You can use:




array('Attribute Name', 'on'=>'insert', 'Your validation procedure here'),



You can do some logic in your validation procedure then. Also, kindly check addError method there …

I don’t quite understand what you mean, but if you want to display image in CGridView, you can add a column like this:




array(

  'name'=>'image',

  'type'=>'html',

  'value'=>'CHtml::image(...)',

),



Cheers,

Rei

Hi rei,

Thanks for your reply.

I have fixed first issue. In the second issue I need to display text and images(Up arrow and Down arrow) in the same column. My CGridView code




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'answers-grid',

	'dataProvider'=>$model->search(),

	'columns'=>array(

		array(        

			'name'=>'answer',

		 	'value'=>$model->answer,

			'htmlOptions'=>array('width'=>'420px'),	

		),

		array(        

			'name'=>'default_answer',

		 	'value'=>'Answers::getCorrectAnswerText($data->default_answer)',

			'type'=>'html',		

			'htmlOptions'=>array('style' => 'text-align: center;'),	

		),

		array(        

			'name'=>'published',

		 	'value'=>'Answers::getStatusText($data->published)',

			'type'=>'html',		

			'htmlOptions'=>array('style' => 'text-align: center;'),	

		),

		[b]'ordering',[/b]

		array(

			'class'=>'CButtonColumn',

			'template'=>'{update}{delete}',

			'buttons'=>array

			    (

			        'update' => array

			        (

			            'url'=>'Yii::app()->createUrl("answers/admin", array("aid"=>$data->id,"qid"=>$data->question_id))',

			        ),

			        

			    ),

		),

	),

));



In the ordering column, I need display like {order number) {up arrow image} {down arrow image} in the same column.

Thanks & Regards,

Gangadhar

Have you tried to concatenate them?




array(

  'name'=>'image',

  'type'=>'html',

  'value'=>'CHtml::encode(...) . CHtml::image(...) . CHtml::image(...)',

),



Thanks rei… Now images are displaying in same column.