|
8 | 8 | * - file size
|
9 | 9 | * - file extension
|
10 | 10 | *
|
11 |
| - * @website http://formvalidator.net/ |
| 11 | + * @website http://formvalidator.net/#file-validators |
12 | 12 | * @license Dual licensed under the MIT or GPL Version 2 licenses
|
13 |
| - * @version 2.2.1 |
| 13 | + * @version 2.2.12 |
14 | 14 | */
|
15 | 15 | (function($, window) {
|
16 | 16 |
|
17 | 17 | 'use strict';
|
18 | 18 |
|
19 | 19 | var SUPPORTS_FILE_READER = typeof window.FileReader != 'undefined',
|
20 | 20 |
|
21 |
| - /** |
22 |
| - * @return {Array} |
23 |
| - */ |
24 |
| - _getTypes = function($input) { |
25 |
| - var allowedTypes = $.split( ($input.valAttr('allowing') || '').toLowerCase() ); |
26 |
| - |
27 |
| - if( $.inArray('jpg', allowedTypes) > -1 && $.inArray('jpeg', allowedTypes) == -1) |
28 |
| - allowedTypes.push('jpeg'); |
29 |
| - else if( $.inArray('jpeg', allowedTypes) > -1 && $.inArray('jpg', allowedTypes) == -1) |
30 |
| - allowedTypes.push('jpg'); |
31 |
| - return allowedTypes; |
32 |
| - }, |
| 21 | + /** |
| 22 | + * @return {Array} |
| 23 | + */ |
| 24 | + _getTypes = function($input) { |
| 25 | + var allowedTypes = $.split( ($input.valAttr('allowing') || '').toLowerCase() ); |
| 26 | + if( $.inArray('jpg', allowedTypes) > -1 && $.inArray('jpeg', allowedTypes) == -1) |
| 27 | + allowedTypes.push('jpeg'); |
| 28 | + else if( $.inArray('jpeg', allowedTypes) > -1 && $.inArray('jpg', allowedTypes) == -1) |
| 29 | + allowedTypes.push('jpg'); |
| 30 | + return allowedTypes; |
| 31 | + }, |
33 | 32 |
|
34 |
| - /** |
35 |
| - * @param {Object} obj |
36 |
| - * @param {String} key |
37 |
| - * @param {String} insert |
38 |
| - * @param {Object} lang |
39 |
| - */ |
40 |
| - _generateErrorMsg = function(obj, key, insert, lang) { |
41 |
| - var msg = lang[key] || ''; |
42 |
| - obj.errorMessageKey = ''; // only use message attached to this object |
43 |
| - obj.errorMessage = msg.replace('\%s', insert); |
44 |
| - }, |
| 33 | + /** |
| 34 | + * @param {Object} obj |
| 35 | + * @param {String} key |
| 36 | + * @param {String} insert |
| 37 | + * @param {Object} lang |
| 38 | + */ |
| 39 | + _generateErrorMsg = function(obj, key, insert, lang) { |
| 40 | + var msg = lang[key] || ''; |
| 41 | + obj.errorMessageKey = ''; // only use message attached to this object |
| 42 | + obj.errorMessage = msg.replace('\%s', insert); |
| 43 | + }, |
| 44 | + |
| 45 | + /** |
| 46 | + * @param {String} msg |
| 47 | + */ |
| 48 | + _log = function(msg) { |
| 49 | + if( window.console && window.console.log ) { |
| 50 | + window.console.log(msg); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + /** |
| 55 | + * @param {String} imgPath |
| 56 | + * @param {Function} successCallback |
| 57 | + * @param {Function} errCallback |
| 58 | + * @private |
| 59 | + */ |
| 60 | + _loadImage = function(imgPath, successCallback, errCallback) { |
| 61 | + var reader = new FileReader(), |
| 62 | + image = new Image(); |
| 63 | + |
| 64 | + reader.readAsDataURL(imgPath); |
| 65 | + |
| 66 | + reader.onload = function(fileObj) { |
| 67 | + |
| 68 | + image.src = fileObj.target.result; |
| 69 | + |
| 70 | + image.onload = function() { |
| 71 | + successCallback(this); |
| 72 | + }; |
| 73 | + |
| 74 | + image.onerror= function() { |
| 75 | + errCallback(); |
| 76 | + }; |
45 | 77 |
|
46 |
| - /** |
47 |
| - * @param {String} msg |
48 |
| - */ |
49 |
| - _log = function(msg) { |
50 |
| - if( window.console && window.console.log ) { |
51 |
| - window.console.log(msg); |
52 |
| - } |
53 | 78 | };
|
| 79 | + }; |
54 | 80 |
|
55 | 81 | /*
|
56 | 82 | * Validate mime type (falls back on validate_extension in older browsers)
|
|
172 | 198 | }
|
173 | 199 | };
|
174 | 200 |
|
| 201 | + var disableFormSubmit = function() { |
| 202 | + return false; |
| 203 | + }; |
| 204 | + |
| 205 | + /** |
| 206 | + * Validate image dimension |
| 207 | + */ |
| 208 | + $.formUtils.addValidator({ |
| 209 | + name : 'dimension', |
| 210 | + validatorFunction : function(val, $input, conf, language, $form) { |
| 211 | + |
| 212 | + if( SUPPORTS_FILE_READER ) { |
| 213 | + var hasCorrectDim = true, |
| 214 | + file = $input.get(0).files || []; |
| 215 | + |
| 216 | + if( $input.attr('data-validation').indexOf('mime') == -1) { |
| 217 | + alert('You should validate file type being jpg, gif or png on input '+$input[0].name); |
| 218 | + return false; |
| 219 | + } |
| 220 | + else if( file.length > 1 ) { |
| 221 | + alert('Validating image dimensions does not support inputs allowing multiple files'); |
| 222 | + return false; |
| 223 | + } else if( file.length == 0) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + if( $input.valAttr('has-valid-dim') ) { |
| 228 | + return true; |
| 229 | + } |
| 230 | + else if( $input.valAttr('has-not-valid-dim') ) { |
| 231 | + this.errorMessage = language['wrongFileDim'] + ' '+$input.valAttr('has-not-valid-dim'); |
| 232 | + return false; |
| 233 | + } |
| 234 | + else if($.formUtils.eventType == 'keyup') { |
| 235 | + return null; |
| 236 | + } |
| 237 | + |
| 238 | + var wasFormSubmit = false; |
| 239 | + |
| 240 | + if( $.formUtils.isValidatingEntireForm ) { |
| 241 | + wasFormSubmit = true; |
| 242 | + $.formUtils.haltValidation = true; |
| 243 | + $form |
| 244 | + .bind('submit', disableFormSubmit) |
| 245 | + .addClass('on-blur'); |
| 246 | + } |
| 247 | + |
| 248 | + _loadImage(file[0], function(img) { |
| 249 | + var error = false, |
| 250 | + restrictedDim = {width:0, height:0}, |
| 251 | + getDimRestriction = function(attrVal) { |
| 252 | + var chunks = attrVal.split('x'); |
| 253 | + restrictedDim.width = chunks[0]; |
| 254 | + restrictedDim.height = chunks[1] ? chunks[1] : chunks[0]; |
| 255 | + }, |
| 256 | + minDeclaration = ($input.valAttr('min-dimension') || '').replace('px', ''), |
| 257 | + maxDeclaration = ($input.valAttr('max-dimension') || '').replace('px', ''); |
| 258 | + |
| 259 | + if( minDeclaration ) { |
| 260 | + // check min |
| 261 | + getDimRestriction(minDeclaration); |
| 262 | + if( img.width < restrictedDim.width || img.height < restrictedDim.height ) { |
| 263 | + error = language.imageTooSmall + ' ('+language.min+' '+restrictedDim.width+'x'+restrictedDim.height+'px)'; |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + if( !error && maxDeclaration ) { |
| 268 | + // Check max |
| 269 | + getDimRestriction(maxDeclaration); |
| 270 | + if( img.width > restrictedDim.width || img.height > restrictedDim.height ) { |
| 271 | + if( img.width > restrictedDim.width ) { |
| 272 | + error = language.imageTooWide +' '+restrictedDim.width+'px'; |
| 273 | + } else { |
| 274 | + error = language.imageTooTall +' '+restrictedDim.height+'px'; |
| 275 | + } |
| 276 | + error += ' ('+language.max+' '+restrictedDim.width+'x'+restrictedDim.height+'px)'; |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + // Set validation result flag on input |
| 281 | + if( error ) { |
| 282 | + $input.valAttr('has-not-valid-dim', error); |
| 283 | + } |
| 284 | + else { |
| 285 | + $input.valAttr('has-valid-dim', 'true'); |
| 286 | + } |
| 287 | + |
| 288 | + // Remove validation flag when input changed |
| 289 | + if( !$input.valAttr('has-keyup-event') ) { |
| 290 | + $input |
| 291 | + .valAttr('has-keyup-event', '1') |
| 292 | + .bind('keyup change', function(evt) { |
| 293 | + if( evt.keyCode != 9 && evt.keyCode != 16 ) { |
| 294 | + $(this) |
| 295 | + .valAttr('has-not-valid-dim', false) |
| 296 | + .valAttr('has-valid-dim', false); |
| 297 | + } |
| 298 | + }); |
| 299 | + } |
| 300 | + |
| 301 | + if( wasFormSubmit ) { |
| 302 | + $.formUtils.haltValidation = false; |
| 303 | + $form |
| 304 | + .removeClass('on-blur') |
| 305 | + .get(0).onsubmit = function() {}; |
| 306 | + |
| 307 | + $form.unbind('submit', disableFormSubmit); |
| 308 | + $form.trigger('submit'); // fire submit once more |
| 309 | + console.log('in here'); |
| 310 | + |
| 311 | + } else { |
| 312 | + $input.trigger('blur'); // triggers the validation once more |
| 313 | + } |
| 314 | + |
| 315 | + }, function(err) { |
| 316 | + throw err; |
| 317 | + }); |
| 318 | + |
| 319 | + return true; |
| 320 | + } |
| 321 | + |
| 322 | + return hasCorrectDim; |
| 323 | + }, |
| 324 | + errorMessage : '', |
| 325 | + errorMessageKey: '' // error message created dynamically |
| 326 | + // errorMessageKey: 'wrongFileDim' |
| 327 | + }); |
| 328 | + |
175 | 329 | /*
|
176 | 330 | * This event listener will remove error messages for file
|
177 | 331 | * inputs when file changes
|
|
0 commit comments