yii 如何显示带有验证失败消息的自定义错误消息

问题描述 投票:0回答:2
php yii
2个回答
0
投票

请阅读本文

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


0
投票

如果使用自定义验证函数,则:

 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;
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.