Example #1
0
 function validate($data, &$failed = null)
 {
     $ret = parent::validate($data);
     if ($this->has_file) {
         try {
             $errors = array();
             foreach ($_FILES as $k => $v) {
                 if (isset($v['error']) && $v['error'] == UPLOAD_ERR_NO_FILE) {
                     if ($this->must) {
                         $errors[] = '请选择上传文件!';
                         break;
                     }
                     continue;
                 }
                 $fileExt = Helper_Upload::fileExt($v['name']);
                 if (!in_array(strtolower($fileExt), Helper_Upload::getTypes())) {
                     $errors[] = '上传文件的类型不符合要求';
                 }
                 if ($v['size'] > Helper_Upload::getSize()) {
                     $errors[] = '上传文件的大小超过限制';
                 }
             }
             if (empty($errors)) {
                 return $ret;
             }
             $this[$this->has_file]->invalidate(implode(', ', $errors));
             return false;
         } catch (Exception $ex) {
             $this[$this->has_file]->invalidate($ex->getMessage());
             return false;
         }
     } else {
         return $ret;
     }
 }
Example #2
0
 function validate($data, &$failed = null)
 {
     $ret = parent::validate($data);
     $id = $this->_upload_element->id;
     $uploader = new Helper_Uploader();
     try {
         if (!$uploader->existsFile($id)) {
             if ($this->_skip_upload_enabled) {
                 return $ret;
             }
             throw new QException('没有正确上传文件');
         }
         $file = $uploader->file($id);
         $errors = array();
         if (!$file->isValid($this->_upload_allowed_types)) {
             $errors[] = '上传文件的类型不符合要求';
         }
         if ($file->filesize() > $this->_upload_allowed_size) {
             $errors[] = '上传文件的大小超过限制';
         }
         if (empty($errors)) {
             $this->_upload_element->value = $file;
             return $ret;
         }
         $failed[] = $id;
         $this->_upload_element->invalidate(implode(', ', $errors));
         return false;
     } catch (Exception $ex) {
         $this->_upload_element->invalidate($ex->getMessage());
         return false;
     }
 }