From e0d97a125e1cdfa8fe360f6d1e9b4ef6a4e99761 Mon Sep 17 00:00:00 2001 From: Elad Bezalel Date: Sun, 13 Dec 2015 00:34:18 +0200 Subject: [PATCH] fix(input): show messages with nested forms - Fixed support in Angular 1.3 - Fixed messages when input is in nested forms --- src/components/input/input.js | 11 ++++++--- src/components/input/input.spec.js | 39 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/components/input/input.js b/src/components/input/input.js index 6ce8fc4fbc..9780e03d30 100644 --- a/src/components/input/input.js +++ b/src/components/input/input.js @@ -267,10 +267,13 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) { } var isErrorGetter = containerCtrl.isErrorGetter || function() { - return ngModelCtrl.$invalid && ( - ngModelCtrl.$touched || - (ngModelCtrl.$$parentForm && ngModelCtrl.$$parentForm.$submitted) - ); + return ngModelCtrl.$invalid && (ngModelCtrl.$touched || isParentFormSubmitted()); + }; + + var isParentFormSubmitted = function () { + var parent = $mdUtil.getClosest(element, 'form'); + + return parent ? angular.element(parent).controller('form').$submitted : false; }; scope.$watch(isErrorGetter, containerCtrl.setInvalid); diff --git a/src/components/input/input.spec.js b/src/components/input/input.spec.js index 0da3a8f307..df17379f44 100644 --- a/src/components/input/input.spec.js +++ b/src/components/input/input.spec.js @@ -74,6 +74,45 @@ describe('md-input-container directive', function() { expect(el.find('md-input-container')).toHaveClass('md-input-invalid'); }); + it('should show error on $submitted and $invalid with nested forms', function() { + var template = + '
' + + '
' + + '' + + '' + + '' + + '' + + '
' + + '
'; + + var parentForm = $compile(template)(pageScope); + pageScope.$apply(); + + expect(parentForm.find('md-input-container')).not.toHaveClass('md-input-invalid'); + + var model = parentForm.find('input').controller('ngModel'); + model.$invalid = true; + + var form = parentForm.controller('form'); + form.$submitted = true; + pageScope.$apply(); + + expect(parentForm.find('md-input-container')).toHaveClass('md-input-invalid'); + }); + + it('should not show error on $invalid and not $submitted', function() { + var el = setup('ng-model="foo"', true); + + expect(el.find('md-input-container')).not.toHaveClass('md-input-invalid'); + + var model = el.find('input').controller('ngModel'); + model.$invalid = true; + + pageScope.$apply(); + + expect(el.find('md-input-container')).not.toHaveClass('md-input-invalid'); + }); + it('should show error with given md-is-error expression', function() { var el = $compile( '' +