Skip to content

Additional: max size and number of files #2087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/additional/maxfiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Limit the number of files in a FileList.
$.validator.addMethod( "maxfiles", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}

if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length > param ) {
return false;
}
}

return true;
}, $.validator.format( "Please select no more than {0} files." ) );
18 changes: 18 additions & 0 deletions src/additional/maxsize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Limit the size of each individual file in a FileList.
$.validator.addMethod( "maxsize", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}

if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length ) {
for ( var i = 0; i < element.files.length; i++ ) {
if ( element.files[ i ].size > param ) {
return false;
}
}
}
}

return true;
}, $.validator.format( "File size must not exceed {0} bytes each." ) );
22 changes: 22 additions & 0 deletions src/additional/maxsizetotal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Limit the size of all files in a FileList.
$.validator.addMethod( "maxsizetotal", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}

if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length ) {
var totalSize = 0;

for ( var i = 0; i < element.files.length; i++ ) {
totalSize += element.files[ i ].size;
if ( totalSize > param ) {
return false;
}
}
}
}

return true;
}, $.validator.format( "Total size of all files must not exceed {0} bytes." ) );

111 changes: 111 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ function acceptFileDummyInput( filename, mimeType ) {
};
}

function fileDummyInput( selectedFiles ) {
var aFiles = [],
oFiles;

for ( var i = 0; i < selectedFiles.length; i++ ) {
aFiles.push( {
name: selectedFiles[ i ].name,
size: selectedFiles[ i ].size,
type: "image/jpeg"
} );
}

//Convert the array of objects to an object.
oFiles = aFiles.reduce( function( acc, cur, i ) {
acc[ i ] = cur;
return acc;
}, {} );

//Add the "length" property to the object.
oFiles.length = selectedFiles.length;

//Add the "item()" method to the object.
oFiles.item = function( i ) { return aFiles[ i ]; };

return {
type: "file",
files: oFiles,
nodeName: "INPUT",
value: "/tmp/fake_value",
hasAttribute: function() { return false; }
};
}

QUnit.module( "methods" );

QUnit.test( "default messages", function( assert ) {
Expand Down Expand Up @@ -1704,3 +1737,81 @@ QUnit.test( "file accept - invalid mime type", function( assert ) {
proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" );
assert.equal( proxy(), false, "the selected file for upload has invalid mime type" );
} );

QUnit.test( "file size - below max", function( assert ) {
var input = acceptFileDummyInput( "test.png", "image/png" ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, "500001" );
assert.ok( proxy(), "the selected file for upload is smaller than max" );
} );

QUnit.test( "file size - over max", function( assert ) {
var input = acceptFileDummyInput( "test.png", "image/png" ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, "500000" );
assert.equal( proxy(), false, "the selected file for upload is greater than max" );
} );

QUnit.test( "file maxsize - valid size", function( assert ) {
var selectedFiles = [ { name: "test.jpg", size: 500000 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.ok( proxy(), "the size of the file does not exceed the maximum" );
} );

QUnit.test( "file maxsize - valid size for each file", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.ok( proxy(), "the size of the each file does not exceed the maximum" );
} );

QUnit.test( "file maxsize - too big", function( assert ) {
var selectedFiles = [ { name: "test.jpg", size: 500001 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.equal( proxy(), false, "the size of the file exceeds the maximum" );
} );

QUnit.test( "file maxsize - second file too big", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500001 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.equal( proxy(), false, "the size of the second file exceeds the maximum" );
} );

QUnit.test( "file maxsizetotal - valid size", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 250000 }, { name: "test2.jpg", size: 250000 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsizetotal, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.ok( proxy(), "the size of the files together does not exceed the maximum" );
} );

QUnit.test( "file maxsizetotal - too big", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 250000 }, { name: "test2.jpg", size: 250001 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxsizetotal, new $.validator( {}, $form[ 0 ] ), null, input, 500000 );
assert.equal( proxy(), false, "the size of the files together exceeds the maximum" );
} );

QUnit.test( "file maxfiles - valid number", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxfiles, new $.validator( {}, $form[ 0 ] ), null, input, 2 );
assert.ok( proxy(), "the number of files does not exceed the maximum" );
} );

QUnit.test( "file maxfiles - too many", function( assert ) {
var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 }, { name: "test3.jpg", size: 500000 } ],
input = fileDummyInput( selectedFiles ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.maxfiles, new $.validator( {}, $form[ 0 ] ), null, input, 2 );
assert.equal( proxy(), false, "the number of files exceeds the maximum" );
} );
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy