博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii2.0-rules验证规则应用实例
阅读量:7094 次
发布时间:2019-06-28

本文共 2678 字,大约阅读时间需要 8 分钟。

Rules验证规则:

 required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空.

[['字段名1','字段名2'],required]    //字段1 2 必填[['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息'];

 email : 邮箱验证||CEmailValidator 的别名,确保了特性的值是一个有效的电邮地址.

['email', 'email'];

 match : 正则验证||CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式.

[['字段名'],'match','pattern'=>'正则表达式','message'=>'提示信息'];      [['字段名'],'match','not'=>ture,'pattern'=>'正则表达式','message'=>'提示信息'];  /*正则取反*/

 url : 网址||CUrlValidator 的别名, 确保了特性是一个有效的路径.

['website', 'url', 'defaultScheme' => 'http'];

captcha(验证码)||CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码.

['verificationCode', 'captcha'];

safe : 安全

['description', 'safe'];

compare :(比较) CCompareValidator 的别名, 确保了特性的值等于另一个特性或常量.

['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],//compareValue:比较常量值 operator:比较操作符 ['age', 'compare', 'compareValue' => 30, 'operator' => '>='];

 default : 默认值||CDefaultValueValidator 的别名, 为特性指派了一个默认值.

['age', 'default', 'value' => null];

 exist : 存在||CExistValidator 的别名, 确保属性值存在于指定的数据表字段中.

['字段名', 'exist'];

 file : 文件||CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.

['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]

 filter : 滤镜||CFilterValidator 的别名, 使用一个filter转换属性.

//'skipOnArray' => true 非必填[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true];

 in : 范围||CRangeValidator 的别名, 确保了特性出现在一个预订的值列表里.

['level', 'in', 'range' => [1, 2, 3]];

 unique : 唯一性||CUniqueValidator 的别名, 确保了特性在数据表字段中是唯一的.

['字段名', 'unique']

 integer : 整数

['age', 'integer'];

 number : 数字

['salary', 'number'];

double : 双精度浮点型

['salary', 'double'];

 date : (日期)

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

 string : 字符串

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

 boolean : 是否为一个布尔值||CBooleanValidator 的别名

['字段名', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true];

 image :是否为有效的图片文件

['primaryImage', 'image', 'extensions' => 'png, jpg',  'minWidth' => 100, 'maxWidth' => 1000,  'minHeight' => 100, 'maxHeight' => 1000]

 each:遍历,ids 和 product_ids 是数字的集合

[['ids', 'product_ids'], 'each', 'rule' => ['integer']],

自定义rules:

['password', 'validatePassword'],/** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule */public function validatePassword($attribute, $params){    if (!$this->hasErrors()) {        $user = $this->getUser();        if (!$user || !$user->validatePassword($this->password)) {            $this->addError($attribute, '账号或者密码错误!');        }    }}

 

来源地址:

转载于:https://www.cnblogs.com/yhdsir/p/5208987.html

你可能感兴趣的文章
sql语句select group by order by where一般先后顺序 转载
查看>>
for循环是怎么工作的
查看>>
支付宝支付
查看>>
第三周 动态规划算法(1):1.集合加法
查看>>
iPhone 上怎么给CSS定义 active 样式
查看>>
讨论CGContextDrawImage
查看>>
Servlet基础
查看>>
tomcat+mysql安装配置,项目部署(上)
查看>>
linux sysrq
查看>>
Incorrect NSStringEncoding value 0x0000 detected.
查看>>
(转)as3数组的深复制和浅复制
查看>>
Choose a destination with a supported architecture in order to run on this device.
查看>>
HTML5/CSS3系列教程:HTML5 区域(Sectioning)的重要性
查看>>
Spring Batch学习笔记
查看>>
asp.net mvc 如何在执行完某任务后返回原来页面
查看>>
Oracle: listener.ora 、sqlnet.ora 、tnsnames.ora的配置及例子
查看>>
ASP.NET 中 GridView(网格视图)的使用前台绑定
查看>>
Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换...
查看>>
[转]XBRL应用软件分类
查看>>
C++ 文件的复制、删除、重命名
查看>>