浏览代码

Merge branch 'morrizon-master'

Phuoc Nguyen 11 年之前
父节点
当前提交
5e29fe8d3e
共有 1 个文件被更改,包括 35 次插入3 次删除
  1. 35 3
      src/js/validator/file.js

+ 35 - 3
src/js/validator/file.js

@@ -6,8 +6,12 @@
     $.fn.bootstrapValidator.validators.file = {
         html5Attributes: {
             extension: 'extension',
+            maxfiles: 'maxFiles',
+            minfiles: 'minFiles',
             maxsize: 'maxSize',
             minsize: 'minSize',
+            maxtotalsize: 'maxTotalSize',
+            mintotalsize: 'minTotalSize',
             message: 'message',
             type: 'type'
         },
@@ -19,8 +23,12 @@
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
          * - extension: The allowed extensions, separated by a comma
+         * - maxFiles: The maximum number of files
+         * - minFiles: The minimum number of files
          * - maxSize: The maximum size in bytes
-         * - minSize: the minimum size in bytes
+         * - minSize: The minimum size in bytes
+         * - maxTotalSize: The maximum size in bytes for all files
+         * - minTotalSize: The minimum size in bytes for all files
          * - message: The invalid message
          * - type: The allowed MIME type, separated by a comma
          * @returns {Boolean}
@@ -39,18 +47,37 @@
             if (html5) {
                 // Get FileList instance
                 var files = $field.get(0).files,
-                    total = files.length;
+                    total = files.length,
+                    totalSize = 0;
+
+                // Check the maxFiles
+                if (options.maxFiles && total > parseInt(options.maxFiles, 10)) {
+                  return false;
+                }
+
+                // Check the minFiles
+                if (options.minFiles && total < parseInt(options.minFiles, 10)) {
+                  return false;
+                }
+
                 for (var i = 0; i < total; i++) {
+                    totalSize += files[i].size;
+
                     // Check the minSize
                     if (options.minSize && files[i].size < parseInt(options.minSize, 10)) {
                         return false;
                     }
-                    
+
                     // Check the maxSize
                     if (options.maxSize && files[i].size > parseInt(options.maxSize, 10)) {
                         return false;
                     }
 
+                    // Check the maxTotalSize
+                    if (options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10)) {
+                        return false;
+                    }
+
                     // Check file extension
                     ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
                     if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
@@ -62,6 +89,11 @@
                         return false;
                     }
                 }
+
+                // Check the minTotalSize
+                if (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)) {
+                    return false;
+                }
             } else {
                 // Check file extension
                 ext = value.substr(value.lastIndexOf('.') + 1);