【问题标题】:yii how to show the custom error msg with validation failed msgyii 如何显示带有验证失败消息的自定义错误消息
【发布时间】:2012-07-28 19:02:12
【问题描述】:

我想显示我的自定义错误消息和规则验证失败错误消息。我该怎么做?

来自控制器的这组消息

$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,

这会从视图中获取消息。

<?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/的一些代码

问候 =======================+++++++++++++++++++++++++++++ ++++++===========================

更新问题。

这是我的控制器

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!');
    }

我想将此错误与其他验证失败错误消息一起显示。

【问题讨论】:

    标签: php yii


    【解决方案1】:

    请阅读本文

    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(需要其别名)将用未通过验证的实际属性名称替换此占位符。

    【讨论】:

    • 感谢您的快速回复。但这不是我想要的。我有 2 个字段,通知日期和到期日期。如果(到期日期
    【解决方案2】:

    如果你使用自定义验证功能,那么:

     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;
         }
     }
    

    【讨论】:

    • 科罗托夫斯克,谢谢您的回复。对不起,你的不是我想要的。我用我的控制器更新了我的问题。请看看并帮助我。 :)
    • 好吧,只要设置字符串“过期日期小于当前日期的通知天数”。到$this-&gt;addError() 并验证expire_date 属性,例如函数check()。最后你的检查方法应该是:paste.ubuntu.com/1121289
    • @tharsoe, Controller: paste.ubuntu.com/1121292 您可以通过 $model->getErrors() 在视图中获取验证错误,并根据需要进行任何布局
    • @Korotovsky,你的 "$this->addError()" 和 "$model->getErrors()" 很好。但你知道,没有什么显示另一个验证错误。我想立即显示验证错误和 checkExpireDate 错误。
    • 石~,伙计,检查这个yiiframework.com/doc/api/1.1/CHtml#errorSummary-detail这是你需要100% :)
    猜你喜欢
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多