Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit 2c1e5d6

Browse files
committed
Fixed issue #28 - unbind all 'blur' affect modules
- Fixed issue #28 - unbind all 'blur' in cancelValidation() might affect other modules
1 parent 86e0b67 commit 2c1e5d6

7 files changed

+41
-21
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.18",
3+
"version": "1.3.19",
44
"authors": [
55
"Ghislain B."
66
],

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ Angular-Validation change logs
1919
1.3.15 (2015-04-08) Fixed issue #23 If multiple forms exist in the app the errors in 1 form affect validation in the other
2020
1.3.16 (2015-04-09) Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
2121
1.3.17 (2015-04-11) Added global `$scope.$validationOptions` object, for now only has the `debounce` property that be used by both the Directive and Service.
22-
1.3.18 (2015-04-19) Fixed issue #20 - Error messages shown on submit are non-understandable, this was fixed using $translate promises instead of $translate.instant(). Fixed a few error display on the validationSummary() and checkFormValidity(). Also merged #27 to add Russian
22+
1.3.18 (2015-04-19) Fixed issue #20 - Error messages shown on submit are non-understandable, this was fixed using $translate promises instead of $translate.instant(). Fixed a few error display on the validationSummary() and checkFormValidity(). Also merged #27 to add Russian
23+
1.3.19 (2015-04-20) Fixed issue #28 - unbind all 'blur' in cancelValidation() might affect other modules

dist/angular-validation.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.18",
3+
"version": "1.3.19",
44
"author": "Ghislain B.",
55
"description": "Angular-Validation Directive and Service (ghiscoding)",
66
"main": "app.js",

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Angular Validation (Directive / Service)
2-
`Version: 1.3.18`
2+
`Version: 1.3.19`
33
### Form validation after user inactivity of default 1sec. (customizable timeout)
44

55
Forms Validation with Angular made easy! Angular-Validation is an angular directive/service with locales (languages) with a very simple approach of defining your `validation=""` directly within your element to validate (input, textarea, etc) and...that's it!!! The directive/service will take care of the rest!
@@ -499,4 +499,5 @@ License
499499
* [1.3.15](https://github.com/ghiscoding/angular-validation/commit/24037e4b2e22658e7e2011c022ba4cca26f391d9) `2015-04-08` Fixed #23 If multiple forms exist in the app the errors in 1 form affect validation in the other
500500
* [1.3.16](https://github.com/ghiscoding/angular-validation/commit/6c419d45bdb00341416d91199003d827259bd5da) `2015-04-09` Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
501501
* [1.3.17](https://github.com/ghiscoding/angular-validation/commit/1283a3a7435c70ec0a355ee273c8479e4b9bdabf) `2015-04-11` Added global `$scope.$validationOptions` [Global Options](#global-options) object, for now only has the `debounce` property that be used by both the Directive and Service.
502-
* [1.3.18](https://github.com/ghiscoding/angular-validation/commit/d4b55741b9635cd5654f44c58c146f4d86b2e512) `2015-04-19` Fixed issue #20 - Error messages shown on submit are non-understandable, this was fixed using $translate promises instead of $translate.instant(). Fixed a few error display on the validationSummary() and checkFormValidity(). Also merged #27 to add Russian
502+
* [1.3.18](https://github.com/ghiscoding/angular-validation/commit/d4b55741b9635cd5654f44c58c146f4d86b2e512) `2015-04-19` Fixed issue #20 - Error messages shown on submit are non-understandable, this was fixed using $translate promises instead of $translate.instant(). Fixed a few error display on the validationSummary() and checkFormValidity(). Also merged #27 to add Russian
503+
* [1.3.19]() `2015-04-20` Fixed issue #28 - unbind all 'blur' in cancelValidation() might affect other modules

src/validation-directive.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// create an object of the common validation
2121
var commonObj = new validationCommon(scope, elm, attrs, ctrl);
2222
var timer;
23+
var blurHandler;
24+
var isValidationCancelled = false;
2325

2426
// construct the functions, it's just to make the code cleaner and put the functions at bottom
2527
var construct = {
@@ -49,17 +51,18 @@
4951

5052
/** Cancel current validation test and blank any leftover error message */
5153
function cancelValidation() {
54+
isValidationCancelled = true;
5255
$timeout.cancel(timer);
5356
commonObj.updateErrorMsg('');
5457
ctrl.$setValidity('validation', true);
55-
elm.unbind('blur'); // unbind onBlur event, if not it will fail when input become dirty & empty
58+
elm.unbind('blur', blurHandler); // unbind onBlur event so that it does not fail on a non-required element that is now dirty & empty
5659
}
5760

5861
/** Validator function to attach to the element, this will get call whenever the input field is updated
5962
* and is also customizable through the (typing-limit) for which inactivity this.timer will trigger validation.
6063
* @param string value: value of the input field
6164
*/
62-
function attemptToValidate(value, event) {
65+
function attemptToValidate(value) {
6366
// pre-validate without any events just to pre-fill our validationSummary with all field errors
6467
// passing false as 2nd argument for not showing any errors on screen
6568
commonObj.validate(value, false);
@@ -68,6 +71,8 @@
6871
if(!commonObj.isFieldRequired() && (value === "" || value === null || typeof value === "undefined")) {
6972
cancelValidation();
7073
return value;
74+
}else {
75+
isValidationCancelled = false;
7176
}
7277

7378
// invalidate field before doing any validation
@@ -77,9 +82,13 @@
7782

7883
// if field is not required and his value is empty, cancel validation and exit out
7984
// onBlur make validation without waiting
80-
elm.bind('blur', function() {
81-
// make the regular validation of the field value
82-
scope.$evalAsync( ctrl.$setValidity('validation', commonObj.validate(value, true)) );
85+
elm.bind('blur', blurHandler = function() {
86+
if(isValidationCancelled) {
87+
return value;
88+
}else {
89+
// make the regular validation of the field value
90+
scope.$evalAsync( ctrl.$setValidity('validation', commonObj.validate(value, true)) );
91+
}
8392
return value;
8493
});
8594

src/validation-service.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ angular
1616
var validationAttrs; // Current Validator attributes
1717
var commonObj; // Object of validationCommon service
1818
var timer; // timer of user inactivity time
19+
var blurHandler;
20+
var isValidationCancelled = false;
1921

2022
// service constructor
2123
var validationService = function() {
@@ -65,11 +67,15 @@ angular
6567
}
6668

6769
// onBlur make validation without waiting
68-
attrs.elm.bind('blur', function(event) {
69-
// re-initialize to use current element & remove waiting time & validate
70-
self.commonObj.initialize(attrs.scope, attrs.elm, attrs, attrs.ctrl);
71-
self.commonObj.typingLimit = 0;
72-
attemptToValidate(self, event.target.value);
70+
attrs.elm.bind('blur', blurHandler = function(event) {
71+
if(isValidationCancelled) {
72+
return;
73+
}else {
74+
// re-initialize to use current element & remove waiting time & validate
75+
self.commonObj.initialize(attrs.scope, attrs.elm, attrs, attrs.ctrl);
76+
self.commonObj.typingLimit = 0;
77+
attemptToValidate(self, event.target.value);
78+
}
7379
});
7480

7581
// merge both attributes but 2nd object (attrs) as higher priority, so that for example debounce property inside `attrs` as higher priority over `validatorAttrs`
@@ -206,6 +212,8 @@ angular
206212
if(!self.commonObj.isFieldRequired() && (value === "" || value === null || typeof value === "undefined")) {
207213
cancelValidation(self);
208214
return value;
215+
}else {
216+
isValidationCancelled = false;
209217
}
210218

211219
// invalidate field before doing any validation
@@ -246,10 +254,11 @@ angular
246254
* @param object obj
247255
*/
248256
function cancelValidation(obj) {
257+
isValidationCancelled = true;
249258
$timeout.cancel(self.timer);
250259
obj.commonObj.updateErrorMsg('');
251260
obj.commonObj.ctrl.$setValidity('validation', true);
252-
obj.commonObj.elm.unbind('blur'); // unbind onBlur event, if not it will fail when input become dirty & empty
261+
obj.commonObj.elm.unbind('blur', blurHandler); // unbind onBlur event so that it does not fail on a non-required element that is now dirty & empty
253262
}
254263

255264
/**
@@ -280,7 +289,7 @@ angular
280289
unbindWatcher();
281290

282291
// also unbind the blur directly applied on element
283-
formElmObj.elm.unbind();
292+
//formElmObj.elm.unbind();
284293

285294
// now to remove any errors, we need to make the element untouched, pristine and remove the validation
286295
// also remove it from the validationSummary list and remove any displayed error

0 commit comments

Comments
 (0)