fix(NgModel): make sure the ngMinlength and ngMaxlength validators use the $validators pipeline

Fixes #6304
This commit is contained in:
Matias Niemelä
2014-04-30 21:13:39 -04:00
parent e63d4253d0
commit 5b8e7ecfeb
2 changed files with 10 additions and 16 deletions

View File

@@ -1004,23 +1004,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
// min length validator
if (attr.ngMinlength) {
var minlength = int(attr.ngMinlength);
var minLengthValidator = function(value) {
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
};
ctrl.$parsers.push(minLengthValidator);
ctrl.$formatters.push(minLengthValidator);
}
// max length validator
if (attr.ngMaxlength) {
var maxlength = int(attr.ngMaxlength);
var maxLengthValidator = function(value) {
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
};
ctrl.$parsers.push(maxLengthValidator);
ctrl.$formatters.push(maxLengthValidator);
}
}

View File

@@ -1345,14 +1345,14 @@ describe('input', function() {
describe('minlength', function() {
it('should invalid shorter than given minlength', function() {
it('should invalidate values that are shorter than the given minlength', function() {
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
changeInputValueTo('aa');
expect(scope.value).toBeUndefined();
expect(inputElm).toBeInvalid();
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
expect(inputElm).toBeValid();
});
it('should listen on ng-minlength when minlength is observed', function() {
@@ -1373,14 +1373,14 @@ describe('input', function() {
describe('maxlength', function() {
it('should invalid shorter than given maxlength', function() {
it('should invalidate values that are longer than the given maxlength', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');
changeInputValueTo('aaaaaaaa');
expect(scope.value).toBeUndefined();
expect(inputElm).toBeInvalid();
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
expect(inputElm).toBeValid();
});
it('should listen on ng-maxlength when maxlength is observed', function() {