30 Star 241 Fork 87

店滴云物联网开源框架 / ddiot-茶室民宿酒店棋牌室管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
验证规则.md 5.04 KB
一键复制 编辑 原始数据 按行查看 历史
wangchunsheng 提交于 2023-07-19 08:57 . 代码兼容修复

验证规则

动态模型验证

use yii\base\DynamicModel;

// 创建一个动态模型
$model = DynamicModel::validateData(['name' => 'John', 'age' => 25], [
    [['name', 'age'], 'required'],
    ['age', 'integer', 'min' => 18],
]);

// 使用动态模型进行数据验证
if ($model->validate()) {
    echo "Validation passed!";
} else {
    echo "Validation failed!";
    print_r($model->getErrors());
}

required : 必须值验证属性

[['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredValidator 的别名, 确保了特性不为空.

email : 邮箱验证

['email', 'email']; #说明:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址. 

match : 正则验证

[['字段名'],match,'pattern'=>'正则表达式','message'=>'提示信息'];      
[['字段名'],match,'not'=>ture,'pattern'=>'正则表达式','message'=>'提示信息']; /*正则取反*/ #说明:CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式. 

url : 网址

['website', 'url', 'defaultScheme' => 'http']; #说明:CUrlValidator 的别名, 确保了特性是一个有效的路径. 

captcha : 验证码

['verificationCode', 'captcha']; #说明:CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码. 

safe : 安全

['description', 'safe'];

compare : 比较

['age', 'compare', 'compareValue' => 30, 'operator' => '>=']; #说明:compareValue(比较常量值) - operator(比较操作符)  #说明:CCompareValidator 的别名,确保了特性的值等于另一个特性或常量. 

array('down_days', 'compare', 'compareAttribute'=>'up_days','message'=>'与上调设置天数不一致')

default : 默认值

['age', 'default', 'value' => null]; #说明:CDefaultValueValidator 的别名, 为特性指派了一个默认值. 

exist : 存在

['username', 'exist']; #说明:CExistValidator 的别名,确保属性值存在于指定的数据表字段中. 

file : 文件

['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]; #说明:CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.

filter : 滤镜

[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true]; #说明:CFilterValidator 的别名, 使用一个filter转换属性. 

in : 范围

['level', 'in', 'range' => [1, 2, 3]]; #说明:CRangeValidator 的别名,确保了特性出现在一个预订的值列表里. 

unique : 唯一性

['username', 'unique'] #说明:CUniqueValidator 的别名,确保了特性在数据表字段中是唯一的.

integer : 整数

['age', 'integer'];

number : 数字

['salary', 'number'];

double : 双精度浮点型

['salary', 'double'];

1

date : 日期

[['from', 'to'], 'date'];

1

string : 字符串

['username', 'string', 'length' => [4, 24]];

1

boolean : 是否为一个布尔值

['字段名', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true]; #说明:CBooleanValidator 的别名 

启动场景的功能:

model类

#设置场景
public function scenarios()
 {
     $scenarios = parent::scenarios();
 	
 	//各个场景的活动属性
     $scenarios['register'] = ['useraccount', 'username', 'password','password_compare'];
     $scenarios['save'] = ['useraccount', 'username', 'password'];
     $scenarios['login'] = ['useraccount','password','verifyCode'];
     $scenarios['dologin'] = ['useraccount','password'];

     return $scenarios;
 
 }

controller类


public function actionRegister() {
 
	$model = new User();
	$model->setScenario('register');

	$id = Yii::$app->user->id;
	// var_dump($id);

	return $this->render('register',['model' => $model]);
}

启用自定验证

array('name', 'rulesCheckLength','max'=>50,'message'=>'规则名称需小于2个字符'),

在model添加对应方法:

public function rulesCheckLength($attribute,$params){
	    
        $str = $this->$attribute;

	    $max = isset($params['max'])&&$params['max']?$params['max']:50;

        if(function_exists('mb_strlen'))
            $length = mb_strlen($str, Yii::app()->charset);
        else
            $length=strlen($str);

        if ($length > $max) {
            $this->addError($attribute, $params['message']);
            return true;
        }
        return false;
    }

unique 唯一性组合验证

// a1 needs to be unique ['a1', 'unique'] // a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value ['a1', 'unique', 'targetAttribute' => 'a2'] // a1 and a2 need to be unique together, and they both will receive error message [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 and a2 need to be unique together, only a1 will receive error message ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value) ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]

PHP
1
https://gitee.com/wayfirer/ddiot.git
git@gitee.com:wayfirer/ddiot.git
wayfirer
ddiot
ddiot-茶室民宿酒店棋牌室管理系统
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891