Skip to content
This repository was archived by the owner on Sep 28, 2019. It is now read-only.

Commit 727a528

Browse files
committed
Update to v1.13.22
1 parent 731f7e7 commit 727a528

5 files changed

+167
-49
lines changed

Scripts/jcs-auto-validate.js

+149-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* angular-auto-validate - v1.4.20 - 2014-10-16
2+
* angular-auto-validate - v1.13.22 - 2014-12-06
33
* https://github.com/jonsamwell/angular-auto-validate
44
* Copyright (c) 2014 Jon Samwell (http://www.jonsamwell.com)
55
*/
@@ -19,6 +19,7 @@
1919
var elementStateModifiers = {},
2020
enableValidElementStyling = true,
2121
enableInvalidElementStyling = true,
22+
validationEnabled = true,
2223

2324
toBoolean = function (value) {
2425
var v;
@@ -54,6 +55,45 @@
5455
return enableInvalidElementStyling && !getBooleanAttributeValue(el, 'disable-invalid-styling');
5556
};
5657

58+
/**
59+
* @ngdoc function
60+
* @name validator#enable
61+
* @methodOf validator
62+
*
63+
* @description
64+
* By default auto validate will validate all forms and elements with an ngModel directive on. By
65+
* setting enabled to false you will explicitly have to opt in to enable validation on forms and child
66+
* elements.
67+
*
68+
* Note: this can be overridden by add the 'auto-validate-enabled="true/false' attribute to a form.
69+
*
70+
* Example:
71+
* <pre>
72+
* app.config(function (validator) {
73+
* validator.enable(false);
74+
* });
75+
* </pre>
76+
*
77+
* @param {Boolean} isEnabled true to enable, false to disable.
78+
*/
79+
this.enable = function (isEnabled) {
80+
validationEnabled = isEnabled;
81+
};
82+
83+
/**
84+
* @ngdoc function
85+
* @name validator#isEnabled
86+
* @methodOf validator
87+
*
88+
* @description
89+
* Returns true if the library is enabeld.
90+
*
91+
* @return {Boolean} true if enabled, otherwise false.
92+
*/
93+
this.isEnabled = function () {
94+
return validationEnabled;
95+
};
96+
5797
/**
5898
* @ngdoc function
5999
* @name validator#setDefaultElementModifier
@@ -229,7 +269,6 @@
229269
};
230270

231271
this.$get = [
232-
233272
function () {
234273
return this;
235274
}
@@ -243,8 +282,8 @@
243282

244283
angular.module('jcs-autoValidate')
245284
.factory('bootstrap3ElementModifier', [
246-
247-
function () {
285+
'$log',
286+
function ($log) {
248287
var reset = function (el) {
249288
angular.forEach(el.find('span'), function (spanEl) {
250289
spanEl = angular.element(spanEl);
@@ -256,16 +295,18 @@
256295
el.removeClass('has-success has-error has-feedback');
257296
},
258297
findWithClassElementAsc = function (el, klass) {
259-
var parent = el;
298+
var retuenEl,
299+
parent = el;
260300
for (var i = 0; i <= 3; i += 1) {
261301
if (parent !== undefined && parent.hasClass(klass)) {
302+
retuenEl = parent;
262303
break;
263304
} else if (parent !== undefined) {
264305
parent = parent.parent();
265306
}
266307
}
267308

268-
return parent;
309+
return retuenEl;
269310
},
270311

271312
findWithClassElementDesc = function (el, klass) {
@@ -334,18 +375,23 @@
334375
*/
335376
makeValid = function (el) {
336377
var frmGroupEl = findFormGroupElement(el),
378+
inputGroupEl;
379+
380+
if (frmGroupEl) {
381+
reset(frmGroupEl);
337382
inputGroupEl = findInputGroupElement(frmGroupEl[0]);
383+
frmGroupEl.addClass('has-success ' + (inputGroupEl.length > 0 ? '' : 'has-feedback'));
384+
if (addValidationStateIcons) {
385+
var iconElText = '<span class="glyphicon glyphicon-ok form-control-feedback"></span>';
386+
if (inputGroupEl.length > 0) {
387+
iconElText = iconElText.replace('form-', '');
388+
iconElText = '<span class="input-group-addon control-feedback">' + iconElText + '</span';
389+
}
338390

339-
reset(frmGroupEl);
340-
frmGroupEl.addClass('has-success ' + (inputGroupEl.length > 0 ? '' : 'has-feedback'));
341-
if (addValidationStateIcons) {
342-
var iconElText = '<span class="glyphicon glyphicon-ok form-control-feedback"></span>';
343-
if (inputGroupEl.length > 0) {
344-
iconElText = iconElText.replace('form-', '');
345-
iconElText = '<span class="input-group-addon control-feedback">' + iconElText + '</span';
391+
insertAfter(el, angular.element(iconElText));
346392
}
347-
348-
insertAfter(el, angular.element(iconElText));
393+
} else {
394+
$log.error('Angular-auto-validate: invalid bs3 form structure elements must be wrapped by a form-group class');
349395
}
350396
},
351397

@@ -363,19 +409,25 @@
363409
*/
364410
makeInvalid = function (el, errorMsg) {
365411
var frmGroupEl = findFormGroupElement(el),
366-
inputGroupEl = findInputGroupElement(frmGroupEl[0]),
367-
helpTextEl = angular.element('<span class="help-block has-error error-msg">' + errorMsg + '</span>');
368-
reset(frmGroupEl, inputGroupEl);
369-
frmGroupEl.addClass('has-error ' + (inputGroupEl.length > 0 ? '' : 'has-feedback'));
370-
insertAfter(inputGroupEl.length > 0 ? inputGroupEl : el, helpTextEl);
371-
if (addValidationStateIcons) {
372-
var iconElText = '<span class="glyphicon glyphicon-remove form-control-feedback"></span>';
373-
if (inputGroupEl.length > 0) {
374-
iconElText = iconElText.replace('form-', '');
375-
iconElText = '<span class="input-group-addon control-feedback">' + iconElText + '</span';
376-
}
412+
helpTextEl = angular.element('<span class="help-block has-error error-msg">' + errorMsg + '</span>'),
413+
inputGroupEl;
377414

378-
insertAfter(el, angular.element(iconElText));
415+
if (frmGroupEl) {
416+
reset(frmGroupEl);
417+
inputGroupEl = findInputGroupElement(frmGroupEl[0]);
418+
frmGroupEl.addClass('has-error ' + (inputGroupEl.length > 0 ? '' : 'has-feedback'));
419+
insertAfter(inputGroupEl.length > 0 ? inputGroupEl : el, helpTextEl);
420+
if (addValidationStateIcons) {
421+
var iconElText = '<span class="glyphicon glyphicon-remove form-control-feedback"></span>';
422+
if (inputGroupEl.length > 0) {
423+
iconElText = iconElText.replace('form-', '');
424+
iconElText = '<span class="input-group-addon control-feedback">' + iconElText + '</span';
425+
}
426+
427+
insertAfter(el, angular.element(iconElText));
428+
}
429+
} else {
430+
$log.error('Angular-auto-validate: invalid bs3 form structure elements must be wrapped by a form-group class');
379431
}
380432
},
381433

@@ -481,7 +533,7 @@
481533
function ($q, $http) {
482534
var currentCulture = 'en-gb',
483535

484-
i18nFileRootPath = 'Scripts/lang',
536+
i18nFileRootPath = 'js/angular-auto-validate/dist/lang',
485537

486538
cultureRetrievalPromise,
487539

@@ -499,7 +551,7 @@
499551
* Set the root path to the il8n files on the server
500552
*
501553
* @param {String} rootPath - The root path on the server to the il8n file - this defaults
502-
* to 'Scripts/lang/'
554+
* to 'js/angular-auto-validate/lang/'
503555
*/
504556
setI18nFileRootPath = function (rootPath) {
505557
i18nFileRootPath = rootPath;
@@ -738,14 +790,33 @@
738790
(function (angular) {
739791
'use strict';
740792

793+
794+
angular.module('jcs-autoValidate')
795+
.factory('jcs-elementUtils', [
796+
function () {
797+
var isElementVisible = function (el) {
798+
return el[0].offsetWidth > 0 && el[0].offsetHeight > 0;
799+
};
800+
801+
return {
802+
isElementVisible: isElementVisible
803+
};
804+
}
805+
]);
806+
741807
angular.module('jcs-autoValidate')
742808
.factory('validationManager', [
743809
'validator',
744-
function (validator) {
810+
'jcs-elementUtils',
811+
function (validator, elementUtils) {
745812
var elementTypesToValidate = ['input', 'textarea', 'select', 'form'],
746813

814+
elementIsVisible = function (el) {
815+
return elementUtils.isElementVisible(el);
816+
},
817+
747818
shouldValidateElement = function (el) {
748-
return el && el.length > 0 && elementTypesToValidate.indexOf(el[0].nodeName.toLowerCase()) > -1;
819+
return el && el.length > 0 && elementIsVisible(el) && elementTypesToValidate.indexOf(el[0].nodeName.toLowerCase()) > -1;
749820
},
750821

751822
/**
@@ -773,17 +844,23 @@
773844
return errorTypeToReturn;
774845
};
775846

776-
if ((forceValidation || shouldValidateElement(el)) && modelCtrl && needsValidation) {
847+
if ((forceValidation || (shouldValidateElement(el) && modelCtrl && needsValidation))) {
777848
isValid = !modelCtrl.$invalid;
778849

779850
if (isValid) {
780851
validator.makeValid(el);
781852
} else {
782853
errorType = findErrorType(modelCtrl.$error);
783854

784-
validator.getErrorMessage(errorType, el).then(function (errorMsg) {
785-
validator.makeInvalid(el, errorMsg);
786-
});
855+
if (errorType === undefined) {
856+
// we have a weird situation some users are encountering where a custom control
857+
// is valid but the ngModel is report it isn't and thus no valid error type can be found
858+
isValid = true;
859+
} else {
860+
validator.getErrorMessage(errorType, el).then(function (errorMsg) {
861+
validator.makeInvalid(el, errorMsg);
862+
});
863+
}
787864
}
788865
}
789866

@@ -934,16 +1011,19 @@
9341011
(function (angular) {
9351012
'use strict';
9361013

937-
angular.module('jcs-autoValidate').directive('disableDynamicValidation', [
938-
939-
function () {
1014+
angular.module('jcs-autoValidate').directive('form', [
1015+
'validator',
1016+
function (validator) {
9401017
return {
941-
restrict: 'A',
1018+
restrict: 'E',
9421019
require: 'form',
9431020
compile: function () {
9441021
return {
9451022
pre: function (scope, element, attrs, ctrl) {
946-
ctrl.disableDynamicValidation = true;
1023+
ctrl.disableDynamicValidation = !validator.isEnabled();
1024+
if (attrs.disableDynamicValidation !== undefined) {
1025+
ctrl.disableDynamicValidation = attrs.disableDynamicValidation === undefined || attrs.disableDynamicValidation === '' || attrs.disableDynamicValidation === 'true';
1026+
}
9471027
}
9481028
};
9491029
}
@@ -968,7 +1048,7 @@
9681048
return function (scope, element) {
9691049
element.on('submit', function (event) {
9701050
scope.$apply(function () {
971-
if (force === true || validationManager.validateForm(element)) {
1051+
if (validationManager.validateForm(element) || force === true) {
9721052
fn(scope, {
9731053
$event: event
9741054
});
@@ -1022,7 +1102,7 @@
10221102
ngModelOptions = ngModelCtrl.$options === undefined ? undefined : ngModelCtrl.$options;
10231103
}
10241104

1025-
if (attrs.formnovalidate === undefined || (frmCtrl !== undefined && frmCtrl.disableDynamicValidation !== true)) {
1105+
if (attrs.formnovalidate === undefined || (frmCtrl !== undefined && frmCtrl.disableDynamicValidation === false)) {
10261106
if (supportsNgModelOptions || ngModelOptions === undefined || ngModelOptions.updateOn === undefined || ngModelOptions.updateOn === '') {
10271107
ngModelCtrl.$setValidity = function (validationErrorKey, isValid) {
10281108
setValidity.call(ngModelCtrl, validationErrorKey, isValid);
@@ -1055,12 +1135,28 @@
10551135

10561136
ngModelCtrl.setExternalValidation = function (errorMsgKey, errorMessage, addToModelErrors) {
10571137
if (addToModelErrors) {
1058-
ngModelCtrl.$errors[errorMsgKey] = false;
1138+
if (ngModelCtrl.$error) {
1139+
ngModelCtrl.$error[errorMsgKey] = false;
1140+
} else {
1141+
ngModelCtrl.$errors[errorMsgKey] = false;
1142+
}
10591143
}
10601144

10611145
validationManager.setElementValidationError(element, errorMsgKey, errorMessage);
10621146
};
10631147

1148+
ngModelCtrl.removeExternalValidation = function (errorMsgKey, addToModelErrors) {
1149+
if (addToModelErrors) {
1150+
if (ngModelCtrl.$error) {
1151+
ngModelCtrl.$error[errorMsgKey] = true;
1152+
} else {
1153+
ngModelCtrl.$errors[errorMsgKey] = true;
1154+
}
1155+
}
1156+
1157+
validationManager.resetElement(element);
1158+
};
1159+
10641160
if (frmCtrl) {
10651161
frmCtrl.setExternalValidation = function (modelProperty, errorMsgKey, errorMessageOverride, addToModelErrors) {
10661162
var success = false;
@@ -1071,6 +1167,16 @@
10711167

10721168
return success;
10731169
};
1170+
1171+
frmCtrl.removeExternalValidation = function (modelProperty, errorMsgKey, errorMessageOverride, addToModelErrors) {
1172+
var success = false;
1173+
if (frmCtrl[modelProperty]) {
1174+
frmCtrl[modelProperty].removeExternalValidation(errorMsgKey, addToModelErrors);
1175+
success = true;
1176+
}
1177+
1178+
return success;
1179+
};
10741180
}
10751181
};
10761182
};

0 commit comments

Comments
 (0)