我想显示我的自定义错误消息和规则验证失败错误消息。我该怎么做?
来自 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,
这从 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; ?>
我使用了 http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/ 中的一些代码
问候 =======================+++++++++++++++++++++++++++++++++==========================
更新问题。
这是我的 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!');
}
我想用其他验证失败错误消息显示此错误。
最佳答案
请阅读此内容
http://www.yiiframework.com/wiki/1/how-to-customize-the-error-message-of-a-validation-rule/
或者试试
class Post extends CActiveRecord
{
public function rules()
{
return array(
array('title, content', 'required',
'message'=>'Please enter a value for {attribute}.'),
// ... other rules
);
}
}
在上面,自定义的错误消息包含一个预定义的占位符{attribute}。 CRequiredValidator(其别名是必需的)将用未通过验证的实际属性名称替换此占位符。
关于php - yii 如何显示带有验证失败消息的自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11732887/