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.