Reputation: 2063
I would like to show my custom error msg with rule validation fail error msg. How can I do it?
this set msg from controller
$expire_date_error = '<p>Please enter the company license expire date more than notification days</p>
<ul>
<li>Expire Date is less than notificaion days on current date.</li>
</ul>';
Yii::app()->user->setFlash('expire_date_error',$expire_date_error);
$this->render('create',array(
'model'=>$model,
this get msg from view.
<?php if(Yii::app()->user->hasFlash('expire_date_error')):?>
<div class="errorMessage">
<?php echo Yii::app()->user->getFlash('expire_date_error'); ?>
</div>
<?php endif; ?>
I used some code from http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/
regards =======================+++++++++++++++++++++++++++++++++==========================
Update question.
This is my controller
public function actionCreate()
{
$model=new CompanyLicense;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['CompanyLicense']))
{
$currentTime = date('Y-m-d h:i:s', time());
$model->attributes= $_POST['CompanyLicense'];
$model->created = $currentTime;
$model->active = 1;
$model->expire_date= $_POST['CompanyLicense']['expire_date'];
if($model->checkExpireDate())
{
$model->file_name=CUploadedFile::getInstance($model,'file_name');
$folder = Yii::getPathOfAlias('webroot').'/images/';
if($model->save())
{
mkdir($folder.$model->id);
$model->file_name->saveAs($folder. $model->id . "/" . $model->file_name);
$this->redirect(array('view','id'=>$model->id));
}
}else{
//echo 'debug';
$expire_date_error = '<p>Please enter the company license expire date more than notification days</p>
<ul>
<li>Expire Date is less than notificaion days on current date.</li>
</ul>';
Yii::app()->user->setFlash('expire_date_error',$expire_date_error);
}
}
$this->render('create',array(
'model'=>$model,
));
}
public function checkExpireDate($attribute='',$params ='')
{
$currentTime = date('Y-m-d', time());
$currentTime = date('Y-m-d', strtotime($currentTime . '+ ' . $this->notification_days.' days'));
if ($currentTime > $this->expire_date)
return false;
return true;
//$this->addError($this->expire_date,'should be more ' . $this->notification_days . ' days on current date!');
}
I want to show this error with other validation failed error msg.
Upvotes: 3
Views: 16854
Reputation: 586
If you use the custom validation function, then:
class Post extends CActiveRecord
{
public function rules()
{
return array(
array('expire_date_error', 'check'),
);
}
public function check()
{
if($this->a > $this->b) {
$this->addError('expire_date_error', 'New custom error message');
return false;
}
return true;
}
}
Upvotes: 0
Reputation: 7762
Please read this
http://www.yiiframework.com/wiki/1/how-to-customize-the-error-message-of-a-validation-rule/
or try
class Post extends CActiveRecord
{
public function rules()
{
return array(
array('title, content', 'required',
'message'=>'Please enter a value for {attribute}.'),
// ... other rules
);
}
}
In the above, the customized error message contains a predefined placeholder {attribute}. The CRequiredValidator (whose alias is required) will replace this placeholder with the actual attribute name that fails the validation.
Upvotes: 0