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

Commit 74fe691

Browse files
EladBezalelThomasBurleson
authored andcommitted
fix(input): show messages with nested forms
- Fixed support in Angular 1.3 - Fixed messages when input is in nested forms Closes #6276. Fixes #6699.
1 parent 1b7b557 commit 74fe691

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"keywords": "client-side, browser, material, material-design, design, angular, css, components, google",
66
"homepage": "https://material.angularjs.org",
77
"bugs": "https://github.com/angular/material/issues",
8+
"license": "MIT",
89
"licenses": [
910
{
1011
"type": "MIT",
@@ -16,13 +17,13 @@
1617
"url": "git://github.com/angular/material.git"
1718
},
1819
"devDependencies": {
19-
"angular": "^1.4.7",
20-
"angular-animate": "^1.4.7",
21-
"angular-aria": "^1.4.7",
22-
"angular-messages": "^1.4.7",
23-
"angular-mocks": "^1.4.7",
24-
"angular-route": "^1.4.7",
25-
"angular-sanitize": "^1.4.7",
20+
"angular": "^1.4.8",
21+
"angular-animate": "^1.4.8",
22+
"angular-aria": "^1.4.8",
23+
"angular-messages": "^1.4.8",
24+
"angular-mocks": "^1.4.8",
25+
"angular-route": "^1.4.8",
26+
"angular-sanitize": "^1.4.8",
2627
"angular-touch": "^1.4.8",
2728
"angularytics": "^0.3.0",
2829
"canonical-path": "0.0.2",
@@ -79,4 +80,4 @@
7980
"merge-base": "git merge-base $(npm run -s current-branch) origin/master",
8081
"squash": "git rebase -i $(npm run -s merge-base)"
8182
}
82-
}
83+
}

src/components/input/input.js

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

269269
var isErrorGetter = containerCtrl.isErrorGetter || function() {
270-
return ngModelCtrl.$invalid && (
271-
ngModelCtrl.$touched ||
272-
(ngModelCtrl.$$parentForm && ngModelCtrl.$$parentForm.$submitted)
273-
);
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;
274277
};
275278

276279
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)