yii2 rules 验证规则

yii2  框架定义的约束

     public $builtInValidators = [
    ‘boolean‘ => ‘yii\validators\BooleanValidator‘,
    ‘captcha‘ => ‘yii\captcha\CaptchaValidator‘,
    ‘compare‘ => ‘yii\validators\CompareValidator‘,
    ‘date‘ => ‘yii\validators\DateValidator‘,
    ‘default‘ => ‘yii\validators\DefaultValueValidator‘,
    ‘double‘ => ‘yii\validators\NumberValidator‘,
    ‘each‘ => ‘yii\validators\EachValidator‘,
    ‘email‘ => ‘yii\validators\EmailValidator‘,
    ‘exist‘ => ‘yii\validators\ExistValidator‘,
    ‘file‘ => ‘yii\validators\FileValidator‘,
    ‘filter‘ => ‘yii\validators\FilterValidator‘,
    ‘image‘ => ‘yii\validators\ImageValidator‘,
    ‘in‘ => ‘yii\validators\RangeValidator‘,
    ‘integer‘ => [‘class‘ => ‘yii\validators\NumberValidator‘, ‘integerOnly‘ => true],
    ‘match‘ => ‘yii\validators\RegularExpressionValidator‘,
    ‘number‘ => ‘yii\validators\NumberValidator‘,
    ‘required‘ => ‘yii\validators\RequiredValidator‘,
    ‘safe‘ => ‘yii\validators\SafeValidator‘,
    ‘string‘ => ‘yii\validators\StringValidator‘,
    ‘trim‘ => [‘class‘ => ‘yii\validators\FilterValidator‘, ‘filter‘ => ‘trim‘, ‘skipOnArray‘ => true],
    ‘unique‘ => ‘yii\validators\UniqueValidator‘, ‘url‘ => ‘yii\validators\UrlValidator‘]

rules常用规则

      return $extends= [
                [
                    [‘buyer_id‘,‘linkman‘,‘phone_area‘,‘buyer_id‘,‘province‘,‘city‘,‘street‘,‘zcode‘,‘is_default‘,‘type‘],
                    ‘required‘,‘message‘=>‘{attribute} 不能为空‘,‘on‘=>[‘add‘,‘edit‘]
                ],
                [‘addr_id‘,‘required‘,‘message‘=>‘{attribute} 不能为空‘,‘on‘=>‘edit‘],
                [[‘province‘,‘city‘,‘linkman‘],‘match‘,‘pattern‘=>‘/^[\x{4e00}-\x{9fa5}]+$/u‘,‘message‘ => ‘{attribute}只能输入中文字符‘,],
                [[‘zcode‘,‘is_default‘,‘type‘,‘phone_area‘,‘phone‘,‘zcode‘,‘phone_ext‘,‘mobile‘],‘match‘,‘pattern‘=>‘/^[0-9]+$/‘,‘message‘ => ‘{attribute} 必须为数字‘],
                [‘street‘, ‘string‘, ‘max‘ => 30],
                [‘mobile‘, ‘string‘, ‘max‘ => 11,‘min‘=>11,‘tooLong‘=>‘手机号格式为11位的数字‘],
                [[‘phone_ext‘,‘phone_area‘], ‘string‘, ‘max‘ => 4],
                [[‘phone‘], ‘string‘, ‘max‘ => 10,‘min‘],
                [‘zcode‘,‘string‘,‘max‘=>6,]
            ];
return array(

    //必须填写
    array(‘email, username, password,agree,verifyPassword,verifyCode‘, ‘required‘),

    //检查用户名是否重复
    array(‘email‘,‘unique‘,‘message‘=>‘用户名已占用‘),

    //用户输入最大的字符限制
    array(‘email, username‘, ‘length‘, ‘max‘=>64),

    //限制用户最小长度和最大长度
    array(‘username‘, ‘length‘, ‘max‘=>7, ‘min‘=>2, ‘tooLong‘=>‘用户名请输入长度为4-14个字符‘, ‘tooShort‘=>‘用户名请输入长度为2-7个字‘),

    //限制密码最小长度和最大长度
    array(‘password‘, ‘length‘, ‘max‘=>22, ‘min‘=>6, ‘tooLong‘=>‘密码请输入长度为6-22位字符‘, ‘tooShort‘=>‘密码请输入长度为6-22位字符‘),

    //判断用户输入的是否是邮件
    array(‘email‘,‘email‘,‘message‘=>‘邮箱格式错误‘),

    //检查用户输入的密码是否是一样的
    array(‘verifyPassword‘, ‘compare‘, ‘compareAttribute‘=>‘password‘, ‘message‘=>‘请再输入确认密码‘),

    //检查用户是否同意协议条款
    array(‘agree‘, ‘required‘, ‘requiredValue‘=>true,‘message‘=>‘请确认是否同意隐私权协议条款‘),

    //判断是否是日期格式
    array(‘created‘, ‘date‘, ‘format‘=>‘yyyy/MM/dd/ HH:mm:ss‘),
   //判断是否包含输入的字符
    array(‘superuser‘, ‘in‘, ‘range‘ => array(0, 1)),
  //正则验证器:
    array(‘name‘,‘match‘,‘pattern‘=>‘/^[a-z0-9\-_]+$/‘),
  //数字验证器:
    array(‘id‘, ‘numerical‘, ‘min‘=>1, ‘max‘=>10, ‘integerOnly‘=>true),
   //类型验证 integer,float,string,array,date,time,datetime
    array(‘created‘, ‘type‘, ‘datetime‘),
  //文件验证:
    array(‘filename‘, ‘file‘, ‘allowEmpty‘=>true, ‘types‘=>‘zip, rar, xls, pdf, ppt‘,‘tooLarge‘=>‘图片不要超过800K‘),

    array(‘url‘,
          ‘file‘,    //定义为file类型
          ‘allowEmpty‘=>true,
          ‘types‘=>‘jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx‘,   //上传文件的类型
          ‘maxSize‘=>1024*1024*10,    //上传大小限制,注意不是php.ini中的上传文件大小
          ‘tooLarge‘=>‘文件大于10M,上传失败!请上传小于10M的文件!‘
         ),  
时间: 2024-10-18 17:29:59

yii2 rules 验证规则的相关文章

Yii2 rules验证规则

Rules验证规则:  required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空. [['字段名1','字段名2'],required]    //字段1 2 必填 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息'];  email : 邮箱验证||CEmailValidator 的别名,确保了特性的值是一个有效的电邮地址. ['email', 'email'];  match 

yii2 rules验证规则,ajax验证手机号码是否唯一

<?php namespace frontend\models; use Yii; use yii\base\Model; /** * Signup form */ class SignupForm extends Model { public $mobile; public $captcha; public $password; /** * @inheritdoc */ public function rules() { return [ [['mobile', 'captcha', 'pas

Yii CModel中rules验证规则

array( array(‘username’, ‘required’),  array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12),  array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’),  array(‘password’, ‘authenticate’, ‘on’=>’login’), array(‘Price’,’num

Yii CModel中rules验证规则[转]

array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’), array(‘password’, ‘authenticate’, ‘on’=>’login’), array(‘Price’,’numeri

Yii2.0 rules验证规则大全

required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredValidator 的别名, 确保了特性不为空. email : 邮箱验证 ['email', 'email']; #说明:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址. match : 正则验证 [['字段名'],match,'pattern'=>'正则表达式','messa

[moka收藏]Yii2.0 rules验证规则

required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredValidator 的别名, 确保了特性不为空. email : 邮箱验证 ['email', 'email']; #说明:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址. match : 正则验证 [['字段名'],'match','pattern'=>'/正则表达式/','m

Yii 中比较常用的rules验证规则记录

查看代码 打印 01 return array( 02   03     //必须填写 04     array('email, username, password,agree,verifyPassword,verifyCode', 'required'), 05   06     //检查用户名是否重复 07     array('email','unique','message'=>'用户名已占用'), 08   09     //用户输入最大的字符限制 10     array('ema

yii2数据验证

1. 客户端验证 yii根据AR类rules()中的验证规则生成相应的Javascript验证代码.通过设置参数enableClientValidation的值来决定是否启用 客户端验证,默认为true,有以下3种方法来设置该参数的值. (1)在视图文件ActiveForm中对整个form进行设置: <?php $form = ActiveForm::begin([ 'enableClientValidation' =>true ]); ?> (2)在视图文件ActiveField中对单

yii rules 转自 安全者 &#187; Yii rules常用规则 ,适用Yii2

安全者 » Yii rules常用规则 ,适用Yii2 public function rules(){return array(//必须填写array('email, username, password,agree,verifyPassword,verifyCode', 'required'),//检查用户名是否重复array('email','unique','message'=>'用户名已占用'),//用户输入最大的字符限制array('email, username', 'length