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

Commit 08bfea1

Browse files
committedMar 16, 2012
fix(forms): Set ng-valid/ng-invalid correctly
1 parent f13dd33 commit 08bfea1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed
 

‎src/directive/input.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -800,22 +800,21 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e
800800

801801
if (isValid) {
802802
if ($error[validationErrorKey]) invalidCount--;
803-
$error[validationErrorKey] = false;
804-
toggleValidCss(isValid);
805803
if (!invalidCount) {
806-
toggleValidCss(isValid, validationErrorKey);
804+
toggleValidCss(true);
807805
this.$valid = true;
808806
this.$invalid = false;
809807
}
810808
} else {
811-
if (!$error[validationErrorKey]) invalidCount++;
812-
$error[validationErrorKey] = true;
813-
toggleValidCss(isValid)
814-
toggleValidCss(isValid, validationErrorKey);
809+
toggleValidCss(false)
815810
this.$invalid = true;
816811
this.$valid = false;
812+
invalidCount++;
817813
}
818814

815+
$error[validationErrorKey] = !isValid;
816+
toggleValidCss(isValid, validationErrorKey);
817+
819818
parentForm.$setValidity(validationErrorKey, isValid, this);
820819
};
821820

‎test/directive/inputSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ describe('ng-model', function() {
269269

270270
dealoc(element);
271271
}));
272+
273+
274+
it('should set invalid classes on init', inject(function($compile, $rootScope) {
275+
var element = $compile('<input type="email" ng-model="value" required />')($rootScope);
276+
$rootScope.$digest();
277+
278+
expect(element).toBeInvalid();
279+
expect(element).toHaveClass('ng-invalid-required');
280+
}));
272281
});
273282

274283

0 commit comments

Comments
 (0)
This repository has been archived.