Question for errorSummary()

Sorry about my engish.

This is rules() in model:




public function rules()

	{

		return array(

			array(

				'level, username, password, createTime, updateTime, publish',

				

				'required',

				'on'=>'create',

			),

			array(

				'level, username, updateTime, publish',

			

				'required',

				'on'=>'update',

			),

			array(

				'username',

				

				'match',

				'pattern'=>'/^[A-Za-z0-9]+$/',

			),

			array(

				'username',

			

				'length',

				'min'=>6,

				'max'=>19,

			),

			array(

				'username',

				

				'unique',

				'className'=>'User',

			),

			array(

				'level, username, publish',

				

				'safe',

				'on'=>'search',

			),

			array(

				'level',

				

				'in',

				'range'=>array(

					1,

					2,

					3,

					4,

				),

			),

			array(

				'publish',

				

				'in',

				'range'=>array(

					0,

					1,

				),

			),

		);

	}

This is actionCreate() & actionUpdate() in controller:




public function actionCreate()

	{

		$model=new User('create');

		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('list'));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

	

	public function actionUpdate()

	{

		$model=$this->loadModel();

		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('list'));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}



And view with both file create & update :




<?php $form=$this->beginWidget('CActiveForm',array(

		'id'=>'user-update-form',

		'enableAjaxValidation'=>false,

	)); ?>


		<p class="note">Fields with <span class="required">*</span> are required.</p>

	

		<?php echo $form->errorSummary($model); ?>

	

		<div class="input">

			<?php echo $form->labelEx($model,'level'); ?>

			<?php echo $form->dropDownList($model,'level',array(

				1=>Yii::t('adminModule.comUser','Super Administrator'),

				2=>Yii::t('adminModule.comUser','Administrator'),

				3=>Yii::t('adminModule.comUser','Manager'),

				4=>Yii::t('adminModule.comUser','Member'),

			)); ?>

			<?php echo $form->error($model,'level'); ?>

		</div>

	

		<div class="input">

			<?php echo $form->labelEx($model,'username'); ?>

			<?php echo $form->textField($model,'username'); ?>

			<?php echo $form->error($model,'username'); ?>

		</div>

		

		<?php if($model->isNewRecord):?>

		<div class="input">

			<?php echo $form->labelEx($model,'password'); ?>

			<?php echo $form->passwordField($model,'password'); ?>

			<?php echo $form->error($model,'password'); ?>

		</div>

		<?php endif;?>

	

		<div class="input">

			<?php echo $form->labelEx($model,'publish'); ?>

			<?php echo $form->dropDownList($model,'publish',array(

				1=>Yii::t('adminModule.comUser','Publish'),

				0=>Yii::t('adminModule.comUser','Un-Publish'),

			)); ?>

			<?php echo $form->error($model,'publish'); ?>

		</div>

		

		<div class="action">

			<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

		</div>


	<?php $this->endWidget(); ?>



I see they have difference in errorSummary.

Create:

Update:

I can’t see error updateTime, I idea is that I will show error with updateTime, What is my wrong?

Thanks.

The difference is the scenario

In your controller


$model=new User('create');

scenario is create, and


$model=new User('update');

scenario is update

now check the model, the value of the index ‘on’ will only use those rules for the current scenario


array(

     'level, username, password, createTime, updateTime, publish',

     'required',

     'on'=>'create',//here, these rules will be applied to create scenario

),

array(

    'level, username, updateTime, publish',

    'required',

    'on'=>'update',//and here to the update scenario

),



Ok, it worked, thanks.

But I can’t see their value when I click link update.

I think that my wrong is in loadModel() so how to I do it.

And this is code in controller.




class UserController extends BController

{

	public function actionIndex()

	{

		$this->actionList();

	}

	

	public function actionList()

	{

		$model=new User('search');

        if(isset($_GET['User']))

            $model->attributes=$_GET['User'];

        $this->render('list',array(

            'model'=>$model,

        ));

	}

	

	public function actionCreate()

	{

		$model=new User('create');

		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('list'));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

	

	public function actionDelete()

	{

		if(Yii::app()->request->isPostRequest)

		{

			$this->loadModel()->delete();


			if(!isset($_GET['ajax']))

				$this->redirect(array('index'));

		}

		else

			$this->redirect(array('index'));

	}

	

	public function actionUpdate()

	{

		$model=$this->loadModel('update');

		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('list'));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}

	

	public function actionUnPublish()

	{		

		$this->actionPublish(0);

	}

	

	public function actionPublish($publish=1)

	{		

		$model=$this->loadModel();

		$model->publish=$publish;

		$model->update(array('publish'));

		$this->redirect(array('list'));

	}

	

	public function loadModel($scenario=null)

	{

		if(isset($_GET['id']))

		{

			$model=new User($scenario);

			$_model=$model->findByPk($_GET['id']);

		}

		if($_model===null)

			$this->redirect(array('index'));

			

		return $model;

	}

}



P/s: If I edit line return $model; to return $_model; then I have their value, but I can’t see error of “updateTime”, How to I can do that?

I don’t understand your problem.

In your view seems like that you have no input field for insert create/update time, therefore what for you want to display an error? How will the user fill this field?

Anyway, in the action update update time is already set in the database, if you want to force the user to insert one more time the update time, blank this field in the controller like that:





       public function actionUpdate()

        {

                $model=$this->loadModel('update');

                $model->updateTime=null;

                if(isset($_POST['User']))

                {

                        $model->attributes=$_POST['User'];

                        if($model->save())

                                $this->redirect(array('list'));

                }




Thanks for quick reply.

Yeah, I know that, I added it in view:


<div class="input">

			<?php echo $form->labelEx($model,'updateTime'); ?>

			<?php echo $form->textField($model,'updateTime'); ?>

			<?php echo $form->error($model,'updateTime'); ?>

		</div>

And… I remove it in html by firebug and I can’t got error of it.

That mean is, maybe I should write code:


public function actionUpdate()

        {

                $model=$this->loadModel('update');

                if(isset($_POST['User']))

                {

			if( $_POST['User']['updateTime'] === null || $_POST['User']['xxx'] === null )

			redirect(array('index'));


                        $model->attributes=$_POST['User'];

                        if($model->save())

                                $this->redirect(array('list'));

                }

Right?

You cannot get the error because the value is set in database, so the required validator doesn’t find anything wrong, just try cleaning the value right after retriving module from db:




$model=$this->loadModel('update');

$model->updateTime=null;



Oh my goddddddddd… Thankssssssss. I got it.