yii2的验证规则

来源:互联网 发布:java 动态绑定 编辑:程序博客网 时间:2024/04/27 08:27

/*Yii2.0 rules*/

 

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息'];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['email''email'];  


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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [['字段名'],match,'pattern'=>'正则表达式','message'=>'提示信息'];      
  2.   
  3. [['字段名'],match,'not'=>ture,'pattern'=>'正则表达式','message'=>'提示信息'];  /*正则取反*/  


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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['website''url''defaultScheme' => 'http'];  
 

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['verificationCode''captcha'];  

// safe : 安全

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['description''safe'];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['password''compare']  
  2.   
  3. ['age''compare''compareValue' => 30, 'operator' => '>='];  
  4.                      比较常量值          比较操作符   

 

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['age''default''value' => null];       

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['字段名''exist'];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['primaryImage''file''extensions' => ['png''jpg''gif'], 'maxSize' => 1024*1024*1024];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [['username''email'], 'filter''filter' => 'trim''skipOnArray' => true];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['level''in''range' => [1, 2, 3]];  

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['字段名', 'unique']  

// integer : 整数

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['age''integer'];  

// number : 数字

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['salary''number'];  

// double : 双精度浮点型

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['salary''double'];  

// date : (日期)

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [['from''to'], 'date'];  
 

// string : 字符串

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['username''string''length' => [4, 24]];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['字段名''boolean''trueValue' => true, 'falseValue' => false, 'strict' => true];  

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

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ['primaryImage''image''extensions' => 'png, jpg',  
  2.   
  3.     'minWidth' => 100, 'maxWidth' => 1000,  
  4.   
  5.     'minHeight' => 100, 'maxHeight' => 1000,  
  6.   
  7. ]  
0 0
原创粉丝点击