yii2 rules 验证规则

时间:2022-08-19 18:20:17

  1. yii2  框架定义的约束

    1. public $builtInValidators = [
    2. 'boolean' => 'yii\validators\BooleanValidator',
    3. 'captcha' => 'yii\captcha\CaptchaValidator',
    4. 'compare' => 'yii\validators\CompareValidator',
    5. 'date' => 'yii\validators\DateValidator',
    6. 'default' => 'yii\validators\DefaultValueValidator',
    7. 'double' => 'yii\validators\NumberValidator',
    8. 'each' => 'yii\validators\EachValidator',
    9. 'email' => 'yii\validators\EmailValidator',
    10. 'exist' => 'yii\validators\ExistValidator',
    11. 'file' => 'yii\validators\FileValidator',
    12. 'filter' => 'yii\validators\FilterValidator',
    13. 'image' => 'yii\validators\ImageValidator',
    14. 'in' => 'yii\validators\RangeValidator',
    15. 'integer' => ['class' => 'yii\validators\NumberValidator', 'integerOnly' => true],
    16. 'match' => 'yii\validators\RegularExpressionValidator',
    17. 'number' => 'yii\validators\NumberValidator',
    18. 'required' => 'yii\validators\RequiredValidator',
    19. 'safe' => 'yii\validators\SafeValidator',
    20. 'string' => 'yii\validators\StringValidator',
    21. 'trim' => ['class' => 'yii\validators\FilterValidator', 'filter' => 'trim', 'skipOnArray' => true],
    22. 'unique' => 'yii\validators\UniqueValidator', 'url' => 'yii\validators\UrlValidator']

    rules常用规则

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




    //copy
    yii2 rules 验证规则
    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'),

    yii2 rules 验证规则

    //copy

    yii2 rules 验证规则
      //判断是否包含输入的字符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的文件!'
    ),
    yii2 rules 验证规则





  2. public function rules()
  3. {
  4. return array(
  5. //必须填写
  6. array('email, username, password,agree,verifyPassword,verifyCode', 'required'),
  7. //检查用户名是否重复
  8. array('email','unique','message'=>'用户名已占用'),
  9. //用户输入最大的字符限制
  10. array('email, username', 'length', 'max'=>64),
  11. //限制用户最小长度和最大长度
  12. array('username', 'length', 'max'=>7, 'min'=>2, 'tooLong'=>'用户名请输入长度为4-14个字符', 'tooShort'=>'用户名请输入长度为2-7个字'),
  13. //限制密码最小长度和最大长度
  14. array('password', 'length', 'max'=>22, 'min'=>6, 'tooLong'=>'密码请输入长度为6-22位字符', 'tooShort'=>'密码请输入长度为6-22位字符'),
  15. //判断用户输入的是否是邮件
  16. array('email','email','message'=>'邮箱格式错误'),
  17. //检查用户输入的密码是否是一样的
  18. array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'请再输入确认密码'),
  19. //检查用户是否同意协议条款
  20. array('agree', 'required', 'requiredValue'=>true,'message'=>'请确认是否同意隐私权协议条款'),
  21. //判断是否是日期格式
  22. array('created', 'date', 'format'=>'yyyy/MM/dd/ HH:mm:ss'),
  23. //判断是否包含输入的字符
  24. array('superuser', 'in', 'range' => array(0, 1)),
  25. //正则验证器:
  26. array('name','match','pattern'=>'/^[a-z0-9\-_]+$/'),
  27. //数字验证器:
  28. array('id', 'numerical', 'min'=>1, 'max'=>10, 'integerOnly'=>true),
  29. //类型验证 integer,float,string,array,date,time,datetime
  30. array('created', 'type', 'datetime'),
  31. //文件验证:
  32. array('filename', 'file', 'allowEmpty'=>true, 'types'=>'zip, rar, xls, pdf, ppt','tooLarge'=>'图片不要超过800K'),
  33. array('url',
  34. 'file', //定义为file类型
  35. 'allowEmpty'=>true,
  36. 'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx', //上传文件的类型
  37. 'maxSize'=>1024*1024*10, //上传大小限制,注意不是php.ini中的上传文件大小
  38. 'tooLarge'=>'文件大于10M,上传失败!请上传小于10M的文件!'
  39. ),
  40. } );