Skip to content

Commit

Permalink
fix(NgModel): make sure the ngMinlength and ngMaxlength validators us…
Browse files Browse the repository at this point in the history
…e the $validators pipeline

Fixes angular#6304
  • Loading branch information
matsko committed May 27, 2014
1 parent f790fd4 commit bbcda9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 4 additions & 10 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,23 +1006,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);
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,28 +1274,28 @@ 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();
});
});


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();
});
});

Expand Down

0 comments on commit bbcda9c

Please sign in to comment.