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

Commit

Permalink
fix(forms): Set ng-valid/ng-invalid correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Mar 16, 2012
1 parent f13dd33 commit 08bfea1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,22 +800,21 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e

if (isValid) {
if ($error[validationErrorKey]) invalidCount--;
$error[validationErrorKey] = false;
toggleValidCss(isValid);
if (!invalidCount) {
toggleValidCss(isValid, validationErrorKey);
toggleValidCss(true);
this.$valid = true;
this.$invalid = false;
}
} else {
if (!$error[validationErrorKey]) invalidCount++;
$error[validationErrorKey] = true;
toggleValidCss(isValid)
toggleValidCss(isValid, validationErrorKey);
toggleValidCss(false)
this.$invalid = true;
this.$valid = false;
invalidCount++;
}

$error[validationErrorKey] = !isValid;
toggleValidCss(isValid, validationErrorKey);

parentForm.$setValidity(validationErrorKey, isValid, this);
};

Expand Down
9 changes: 9 additions & 0 deletions test/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ describe('ng-model', function() {

dealoc(element);
}));


it('should set invalid classes on init', inject(function($compile, $rootScope) {
var element = $compile('<input type="email" ng-model="value" required />')($rootScope);
$rootScope.$digest();

expect(element).toBeInvalid();
expect(element).toHaveClass('ng-invalid-required');
}));
});


Expand Down

0 comments on commit 08bfea1

Please sign in to comment.