Skip to content
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<form name="form">
<input type="file" ng-model="files" name="files" multiple accept="image/*, .zip" maxsize="5000" required base-sixty-four-input>
<input type="file" ng-model="files" name="files" multiple showalert="true" accept="image/*, .zip" maxsize="5000" required base-sixty-four-input>
<span ng-show="form.files.$error.maxsize">Files must not exceed 5000 KB</span>
</form>
```
Expand Down
68 changes: 56 additions & 12 deletions src/angular-base64-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,44 @@
// 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) {
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) {
toReturn = [];
}
}

if(attrs.minnum && attrs.showalert && !valid) {
alert('Please select atleast '+parseInt(attrs.minnum)+' files');
}
return val;

return toReturn;
}

function _maxsize(val) {
var valid = true;
var toReturn = val;

if (attrs.maxsize && val) {
var max = parseFloat(attrs.maxsize) * 1000;
Expand All @@ -78,20 +99,30 @@
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;

if(attrs.maxsize && attrs.showalert && !valid) {
alert('You can only select files with size upto '+parseInt(attrs.maxsize)+' KB');
}

return toReturn;
}

function _minsize(val) {
var valid = true;
var toReturn = val;
var min = parseFloat(attrs.minsize) * 1000;

if (attrs.minsize && val) {
Expand All @@ -100,20 +131,29 @@
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;
if(attrs.minsize && attrs.showalert && !valid) {
alert('You can only select files with size greater than '+parseInt(attrs.minsize)+' KB');
}

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, "/.*");
Expand All @@ -128,29 +168,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) {
Expand Down