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

Commit 4936e8d

Browse files
committed
fix(input): show messages with nested forms
- Fixed support in Angular 1.3 - Fixed messages when input is in nested forms
1 parent 16486db commit 4936e8d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/components/input/input.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,13 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
267267
}
268268

269269
var isErrorGetter = containerCtrl.isErrorGetter || function() {
270-
return ngModelCtrl.$invalid && (ngModelCtrl.$touched || ngModelCtrl.$$parentForm.$submitted);
270+
return ngModelCtrl.$invalid && (ngModelCtrl.$touched || isParentFormSubmitted());
271+
};
272+
273+
var isParentFormSubmitted = function () {
274+
var parent = $mdUtil.getClosest(element, 'form');
275+
276+
return parent ? angular.element(parent).controller('form').$submitted : false;
271277
};
272278

273279
scope.$watch(isErrorGetter, containerCtrl.setInvalid);

src/components/input/input.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,45 @@ describe('md-input-container directive', function() {
7474
expect(el.find('md-input-container')).toHaveClass('md-input-invalid');
7575
});
7676

77+
it('should show error on $submitted and $invalid with nested forms', function() {
78+
var template =
79+
'<form>' +
80+
'<div ng-form>' +
81+
'<md-input-container>' +
82+
'<input ng-model="foo">' +
83+
'<label></label>' +
84+
'</md-input-container>' +
85+
'</div>' +
86+
'</form>';
87+
88+
var parentForm = $compile(template)(pageScope);
89+
pageScope.$apply();
90+
91+
expect(parentForm.find('md-input-container')).not.toHaveClass('md-input-invalid');
92+
93+
var model = parentForm.find('input').controller('ngModel');
94+
model.$invalid = true;
95+
96+
var form = parentForm.controller('form');
97+
form.$submitted = true;
98+
pageScope.$apply();
99+
100+
expect(parentForm.find('md-input-container')).toHaveClass('md-input-invalid');
101+
});
102+
103+
it('should not show error on $invalid and not $submitted', function() {
104+
var el = setup('ng-model="foo"', true);
105+
106+
expect(el.find('md-input-container')).not.toHaveClass('md-input-invalid');
107+
108+
var model = el.find('input').controller('ngModel');
109+
model.$invalid = true;
110+
111+
pageScope.$apply();
112+
113+
expect(el.find('md-input-container')).not.toHaveClass('md-input-invalid');
114+
});
115+
77116
it('should show error with given md-is-error expression', function() {
78117
var el = $compile(
79118
'<md-input-container md-is-error="isError">' +

0 commit comments

Comments
 (0)