yii的rules验证规则

  • 图片验证
    public function rules() {
            return [
                [['id'], 'integer'],
                [['id'], 'required'],
                [['files'], 'file', 'maxSize' => 50 * 1024 * 1024, 'maxFiles' => 10, 'mimeTypes' => ['image/*', 'audio/*', 'video/*']],
            ];
        }

     

  • 场景验证
      public function rules() {
            return [
                [['username','password'],'required','on'=>'login']
            ]
     }
    
      public function exec(){
        
        $this->scenario = "login";
         if (!$this->validate()) {
                return [
                    'errors' => $this->errors,
                ];
            }
     }    

     

  • 对指定字段规定范围验证
    public function rules() {
            return [
               ['week', 'in', 'range' => [1,2,3,4,5,6,7]]
    ];

     

posted @ 2019-08-21 10:10  好胖的兔子  阅读(286)  评论(0编辑  收藏  举报