From 9ca825783799959f9f2c1762a9b6367b84d0acfd Mon Sep 17 00:00:00 2001 From: James Crisman Date: Mon, 12 Dec 2016 16:44:16 -0600 Subject: [PATCH 1/3] removes processing of av-val-show because its slow and immediate validation should be asap. provides a way for container-id to work with variables such as an index --- lib/ui/validation/adapter-bootstrap.js | 7 ++++- lib/ui/validation/adapter.js | 4 +-- lib/ui/validation/field.js | 4 +-- lib/ui/validation/form.js | 2 +- lib/ui/validation/tests/form-spec.js | 40 ++++++++++++-------------- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/ui/validation/adapter-bootstrap.js b/lib/ui/validation/adapter-bootstrap.js index 871bde6e..9ad64b94 100644 --- a/lib/ui/validation/adapter-bootstrap.js +++ b/lib/ui/validation/adapter-bootstrap.js @@ -36,7 +36,7 @@ element.parents(AV_BOOTSTRAP_ADAPTER.CLASSES.FORM_GROUP).removeClass(AV_BOOTSTRAP_ADAPTER.CLASSES.ERROR); }, - message: function(element, ngModel) { + message: function(element, ngModel, scope) { var selector = [ '.', @@ -47,7 +47,12 @@ var target = $el.attr(AV_BOOTSTRAP_ADAPTER.SELECTORS.CONTAINER); target = target || $el.attr(AV_BOOTSTRAP_ADAPTER.SELECTORS.DATA_CONTAINER); + // default to siblings + if(scope && target) { + // pew pew! + target = scope.$eval(target); + } target = target ? $('#' + target) : $el.siblings(selector); if(target.length === 0) { diff --git a/lib/ui/validation/adapter.js b/lib/ui/validation/adapter.js index 62dd11c5..81d1b7b5 100644 --- a/lib/ui/validation/adapter.js +++ b/lib/ui/validation/adapter.js @@ -33,8 +33,8 @@ this.adapter.reset(element); }; - proto.message = function(element, ngModel) { - this.adapter.message(element, ngModel); + proto.message = function(element, ngModel, scope) { + this.adapter.message(element, ngModel, scope); }, proto.scroll = function(form) { diff --git a/lib/ui/validation/field.js b/lib/ui/validation/field.js index 9da06cb2..39c3821e 100644 --- a/lib/ui/validation/field.js +++ b/lib/ui/validation/field.js @@ -68,7 +68,7 @@ this.updateView = function() { if(this.ngModel.$dirty || $scope.avValShow) { avValAdapter.element($element, this.ngModel, this.ngModel.avResults.isValid); - avValAdapter.message($element, this.ngModel); + avValAdapter.message($element, this.ngModel, $scope); } }; @@ -143,7 +143,7 @@ var violations = this.ngModel.avResults.violations; violations.splice(0, violations.length); - avValAdapter.message($element, this.ngModel); + avValAdapter.message($element, this.ngModel, $scope); avValAdapter.reset($element); }; diff --git a/lib/ui/validation/form.js b/lib/ui/validation/form.js index 6a4a9529..cef50f07 100644 --- a/lib/ui/validation/form.js +++ b/lib/ui/validation/form.js @@ -125,7 +125,7 @@ // fields inside the form would inherit this behavior. avForm.avValOn = iAttrs.avValOn || null; avForm.avValDebounce = iAttrs.avValDebounce || null; - avForm.avValShow = scope.$eval(iAttrs.avValShow) || null; + avForm.avValShow = iAttrs.avValShow || null; // Allows fields to update with invalid data for dirty form saving avForm.avValInvalid = iAttrs.avValInvalid || false; diff --git a/lib/ui/validation/tests/form-spec.js b/lib/ui/validation/tests/form-spec.js index 674c72c3..fd5c6bbf 100644 --- a/lib/ui/validation/tests/form-spec.js +++ b/lib/ui/validation/tests/form-spec.js @@ -120,40 +120,38 @@ describe('avForm', function() { expect(formGroup.hasClass('has-error')).toBeTruthy(); }); - it('should not immediately create an error if avValShow variable is false', function() { + it('should validate inside ng-repeat', function() { var template = '' + - '
' + - '
' + - '' + + ''+ + '
' + + '' + '

' + '
' + ''; - availity.mock.$scope.demo.showError = false; - + availity.mock.$scope.demo.names = ["charizard", "blastoise", "venasaur", "mewtwo", "mew", "pikachu", "m"]; $el = availity.mock.compileDirective(template); - availity.mock.$scope.$digest(); - - var formGroup = $('#showOnLoadFormGroup'); - expect(formGroup.hasClass('has-error')).toBeFalsy(); + var invalidName = $('#name-6'); + expect(invalidName.hasClass('ng-invalid')).toBeTruthy(); + var validName = $('#name-5'); + expect(validName.hasClass('ng-invalid')).toBeFalsy(); }); - it('should immediately create an error if avValShow variable is true', function() { + it('should validate inside ng-repeat with container-id', function() { var template = '' + - '
' + - '
' + - '' + - '

' + + ''+ + '
' + + '' + + '

' + '
' + ''; - availity.mock.$scope.demo.showError = true; - + availity.mock.$scope.demo.names = ["charizard", "blastoise", "venasaur", "mewtwo", "mew", "pikachu", "m"]; $el = availity.mock.compileDirective(template); - availity.mock.$scope.$digest(); - - var formGroup = $('#showOnLoadFormGroup'); - expect(formGroup.hasClass('has-error')).toBeTruthy(); + var invalidName = $('#name-6'); + expect(invalidName.hasClass('ng-invalid')).toBeTruthy(); + var validName = $('#name-5'); + expect(validName.hasClass('ng-invalid')).toBeFalsy(); }); describe('submit', function() { From 6a887affcdd524fe794abc3aea9cd17423236213 Mon Sep 17 00:00:00 2001 From: James Crisman Date: Wed, 14 Dec 2016 10:31:19 -0600 Subject: [PATCH 2/3] uses attrs instead of scope to get the actual value of the id. and cleans up stuff --- lib/ui/validation/adapter-bootstrap.js | 16 +++------------- lib/ui/validation/adapter.js | 4 ++-- lib/ui/validation/field.js | 4 ++-- lib/ui/validation/tests/form-spec.js | 2 +- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/ui/validation/adapter-bootstrap.js b/lib/ui/validation/adapter-bootstrap.js index 9ad64b94..f34138a3 100644 --- a/lib/ui/validation/adapter-bootstrap.js +++ b/lib/ui/validation/adapter-bootstrap.js @@ -13,10 +13,6 @@ FORM_GROUP: '.form-group:first', NAVBAR: 'navbar-fixed-top' }, - SELECTORS: { - CONTAINER: 'container-id', - DATA_CONTAINER: 'data-container-id' - }, CONTROLLER: '$avValContainerController' }); @@ -36,7 +32,7 @@ element.parents(AV_BOOTSTRAP_ADAPTER.CLASSES.FORM_GROUP).removeClass(AV_BOOTSTRAP_ADAPTER.CLASSES.ERROR); }, - message: function(element, ngModel, scope) { + message: function(element, ngModel, attrs) { var selector = [ '.', @@ -45,15 +41,9 @@ var $el = $(element); - var target = $el.attr(AV_BOOTSTRAP_ADAPTER.SELECTORS.CONTAINER); - target = target || $el.attr(AV_BOOTSTRAP_ADAPTER.SELECTORS.DATA_CONTAINER); + var targetId = attrs.containerId; - // default to siblings - if(scope && target) { - // pew pew! - target = scope.$eval(target); - } - target = target ? $('#' + target) : $el.siblings(selector); + var target = targetId ? $('#' + targetId) : $el.siblings(selector); if(target.length === 0) { $log.warn('avValBootstrapAdapter could not find validation container for {0}', [element]); diff --git a/lib/ui/validation/adapter.js b/lib/ui/validation/adapter.js index 81d1b7b5..ecd03a70 100644 --- a/lib/ui/validation/adapter.js +++ b/lib/ui/validation/adapter.js @@ -33,8 +33,8 @@ this.adapter.reset(element); }; - proto.message = function(element, ngModel, scope) { - this.adapter.message(element, ngModel, scope); + proto.message = function(element, ngModel, attrs) { + this.adapter.message(element, ngModel, attrs); }, proto.scroll = function(form) { diff --git a/lib/ui/validation/field.js b/lib/ui/validation/field.js index 39c3821e..7c4e29a6 100644 --- a/lib/ui/validation/field.js +++ b/lib/ui/validation/field.js @@ -68,7 +68,7 @@ this.updateView = function() { if(this.ngModel.$dirty || $scope.avValShow) { avValAdapter.element($element, this.ngModel, this.ngModel.avResults.isValid); - avValAdapter.message($element, this.ngModel, $scope); + avValAdapter.message($element, this.ngModel, $attrs); } }; @@ -143,7 +143,7 @@ var violations = this.ngModel.avResults.violations; violations.splice(0, violations.length); - avValAdapter.message($element, this.ngModel, $scope); + avValAdapter.message($element, this.ngModel, $attrs); avValAdapter.reset($element); }; diff --git a/lib/ui/validation/tests/form-spec.js b/lib/ui/validation/tests/form-spec.js index fd5c6bbf..3666f5e7 100644 --- a/lib/ui/validation/tests/form-spec.js +++ b/lib/ui/validation/tests/form-spec.js @@ -141,7 +141,7 @@ describe('avForm', function() { var template = '' + '
'+ '
' + - '' + + '' + '

' + '
' + '
'; From 3b193aa6eddacf30becb9bfe0b98ae9365c54345 Mon Sep 17 00:00:00 2001 From: James Crisman Date: Wed, 14 Dec 2016 11:26:43 -0600 Subject: [PATCH 3/3] keeping ability for configurability --- lib/ui/validation/adapter-bootstrap.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ui/validation/adapter-bootstrap.js b/lib/ui/validation/adapter-bootstrap.js index f34138a3..3a7cc245 100644 --- a/lib/ui/validation/adapter-bootstrap.js +++ b/lib/ui/validation/adapter-bootstrap.js @@ -13,6 +13,9 @@ FORM_GROUP: '.form-group:first', NAVBAR: 'navbar-fixed-top' }, + SELECTORS: { + CONTAINER: 'container-id' + }, CONTROLLER: '$avValContainerController' }); @@ -41,7 +44,7 @@ var $el = $(element); - var targetId = attrs.containerId; + var targetId = attrs[attrs.$normalize(AV_BOOTSTRAP_ADAPTER.SELECTORS.CONTAINER)]; var target = targetId ? $('#' + targetId) : $el.siblings(selector);