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

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 #6304
  • Loading branch information
matsko committed Jun 13, 2014
1 parent e63d425 commit 5b8e7ec
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 @@ -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);
}
}

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 @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 5b8e7ec

Please sign in to comment.