From 39b9e6efcee75e67c918bb185b5fe1c7ef5d639f Mon Sep 17 00:00:00 2001 From: Muhammad Salar Khan Date: Sun, 22 Oct 2017 15:17:49 +0500 Subject: [PATCH 1/3] Used validations on the model data. --- src/angular-base64-upload.js | 37 ++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/angular-base64-upload.js b/src/angular-base64-upload.js index bece106..270c6ec 100644 --- a/src/angular-base64-upload.js +++ b/src/angular-base64-upload.js @@ -55,6 +55,9 @@ if (attrs.maxnum && attrs.multiple && val) { var valid = val.length <= parseInt(attrs.maxnum); ngModel.$setValidity('maxnum', valid); + if(!valid) { + return []; + } } return val; } @@ -63,12 +66,16 @@ if (attrs.minnum && attrs.multiple && val) { var valid = val.length >= parseInt(attrs.minnum); ngModel.$setValidity('minnum', valid); + if(!valid) { + return []; + } } return val; } function _maxsize(val) { var valid = true; + var toReturn = val; if (attrs.maxsize && val) { var max = parseFloat(attrs.maxsize) * 1000; @@ -78,20 +85,25 @@ var file = val[i]; if (file.filesize > max) { valid = false; + toReturn = []; break; } } } else { valid = val.filesize <= max; + if(!valid) { + toReturn = null; + } } ngModel.$setValidity('maxsize', valid); } - return val; + return toReturn; } function _minsize(val) { var valid = true; + var toReturn = val; var min = parseFloat(attrs.minsize) * 1000; if (attrs.minsize && val) { @@ -100,20 +112,25 @@ var file = val[i]; if (file.filesize < min) { valid = false; + toReturn = []; break; } } } else { valid = val.filesize >= min; + if(!valid) { + toReturn = null; + } } ngModel.$setValidity('minsize', valid); } - return val; + return toReturn; } function _accept(val) { var valid = true; + var toReturn = val; var regExp, exp, fileExt; if (attrs.accept) { exp = attrs.accept.trim().replace(/[,\s]+/gi, "|").replace(/\./g, "\\.").replace(/\/\*/g, "/.*"); @@ -128,29 +145,33 @@ valid = regExp.test(file.filetype) || regExp.test(fileExt); if (!valid) { + toReturn = []; break; } } } else { fileExt = "." + val.filename.split('.').pop(); valid = regExp.test(val.filetype) || regExp.test(fileExt); + if(!valid) { + toReturn = null; + } } ngModel.$setValidity('accept', valid); } - return val; + return toReturn; } //end validations =============== function _setViewValue() { var newVal = attrs.multiple ? fileObjects : fileObjects[0]; + newVal = _maxsize(newVal); + newVal = _minsize(newVal); + newVal = _maxnum(newVal); + newVal = _minnum(newVal); + newVal = _accept(newVal); ngModel.$setViewValue(newVal); - _maxsize(newVal); - _minsize(newVal); - _maxnum(newVal); - _minnum(newVal); - _accept(newVal); } function _attachHandlerForEvent(eventName, handler, fReader, file, fileObject) { From 9d56685abda8cae5905d71578066a3fc5ebc54ab Mon Sep 17 00:00:00 2001 From: Muhammad Salar Khan Date: Sun, 22 Oct 2017 15:54:23 +0500 Subject: [PATCH 2/3] Validation improved, added user feedback if we now pass showalert="true" the user will get prompted with an alert if a validation fails. --- src/angular-base64-upload.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/angular-base64-upload.js b/src/angular-base64-upload.js index 270c6ec..682fc73 100644 --- a/src/angular-base64-upload.js +++ b/src/angular-base64-upload.js @@ -52,25 +52,39 @@ // VALIDATIONS ========================================================= function _maxnum(val) { + var valid = true; + var toReturn = val; if (attrs.maxnum && attrs.multiple && val) { - var valid = val.length <= parseInt(attrs.maxnum); + valid = val.length <= parseInt(attrs.maxnum); ngModel.$setValidity('maxnum', valid); if(!valid) { - return []; + toReturn = []; } } - return val; + + if(attrs.maxnum && attrs.showalert && !valid) { + alert('You cannot select more than '+parseInt(attrs.maxnum)+' files'); + } + + return toReturn; } function _minnum(val) { + var valid = true; + var toReturn = val; if (attrs.minnum && attrs.multiple && val) { - var valid = val.length >= parseInt(attrs.minnum); + valid = val.length >= parseInt(attrs.minnum); ngModel.$setValidity('minnum', valid); if(!valid) { - return []; + toReturn = []; } } - return val; + + if(attrs.minnum && attrs.showalert && !valid) { + alert('Please select atleast '+parseInt(attrs.minnum)+' files'); + } + + return toReturn; } function _maxsize(val) { @@ -98,6 +112,11 @@ ngModel.$setValidity('maxsize', valid); } + + if(attrs.maxsize && attrs.showalert && !valid) { + alert('You can only select files with size upto '+parseInt(attrs.maxsize)+' KB'); + } + return toReturn; } @@ -125,6 +144,10 @@ ngModel.$setValidity('minsize', valid); } + if(attrs.minsize && attrs.showalert && !valid) { + alert('You can only select files with size greater than '+parseInt(attrs.minsize)+' KB'); + } + return toReturn; } From 0382efc9dde5cc74f74b3f556d450916e9ea4853 Mon Sep 17 00:00:00 2001 From: Muhammad Salar Khan Date: Sun, 22 Oct 2017 15:59:19 +0500 Subject: [PATCH 3/3] Updated documentation --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b1c010e..7209c31 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,12 @@ Validations - `minnum` = Minimum number of items to select (applicable only for multi-select) - `accept` = [Input file accept attribute](http://www.w3schools.com/tags/att_input_accept.asp). `file_extension|audio/*|video/*|image/*|media_type` comma separated - `required` = Checks if the model value is `null`, empty array `[]` or empty object `{}` + - `showalert` = if set to "true" this attribute will allow showing of an alert + window whenever a validation - maxnum, minnum, minsize, maxsize - fails. ```html
- + Files must not exceed 5000 KB
```