Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions src/components/input/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'<form>' +
'<div ng-form>' +
'<md-input-container>' +
'<input ng-model="foo">' +
'<label></label>' +
'</md-input-container>' +
'</div>' +
'</form>';

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(
'<md-input-container md-is-error="isError">' +
Expand Down