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 ngMinlength and ngMaxlength as standalone directives
Browse files Browse the repository at this point in the history
Fixes #6750
  • Loading branch information
matsko committed Jun 13, 2014
1 parent 5b8e7ec commit 26d91b6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
ngChangeDirective,
requiredDirective,
requiredDirective,
minlengthDirective,
minlengthDirective,
maxlengthDirective,
maxlengthDirective,
ngValueDirective,
ngModelOptionsDirective,
ngAttributeAliasDirectives,
Expand Down Expand Up @@ -184,6 +188,10 @@ function publishExternalAPI(angular){
ngChange: ngChangeDirective,
required: requiredDirective,
ngRequired: requiredDirective,
ngMinlength: minlengthDirective,
minlength: minlengthDirective,
ngMaxlength: maxlengthDirective,
maxlength: maxlengthDirective,
ngValue: ngValueDirective,
ngModelOptions: ngModelOptionsDirective
}).
Expand Down
53 changes: 37 additions & 16 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,22 +1000,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
return ctrl.$isEmpty(value) || isUndefined(regexp) || regexp.test(value);
};
}

// min length validator
if (attr.ngMinlength) {
var minlength = int(attr.ngMinlength);
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
};
}

// max length validator
if (attr.ngMaxlength) {
var maxlength = int(attr.ngMaxlength);
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
};
}
}

function weekParser(isoWeek) {
Expand Down Expand Up @@ -2183,6 +2167,43 @@ var requiredDirective = function() {
};


var maxlengthDirective = function() {
return {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;

var maxlength = 0;
attr.$observe('maxlength', function(value) {
maxlength = int(value) || 0;
ctrl.$validate();
});
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
};
}
};
};

var minlengthDirective = function() {
return {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;

var minlength = 0;
attr.$observe('minlength', function(value) {
minlength = int(value) || 0;
ctrl.$validate();
});
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;

This comment has been minimized.

Copy link
@Narretz

Narretz Jun 13, 2014

Contributor

Why is the value valid when it is empty? Empty means basically it has no length, which makes this invalid, no?

edit. Nevermind, I see the logic is that if the value is empty it is valid, and if you need the value you use required.

This comment has been minimized.

Copy link
@matsko

matsko Jun 13, 2014

Author Contributor

@Narretz yes that is correct.

This comment has been minimized.

Copy link
@VRMink

VRMink Aug 12, 2014

@matsko That logic is flawed when you use ng-required. I have an input field which needs a min length, but is only required if another field is not filled out. With the logic we have now, I will need to check the length as part of the ng-required evaluation even though I use an ng-minlength directive as well.

In my opinion this behaviour is a bug.

};
}
};
};


/**
* @ngdoc directive
* @name ngList
Expand Down
36 changes: 36 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,24 @@ describe('input', function() {

expect(value).toBe(5);
});

it('should observe the standard minlength attribute and register it as a validator on the model', function() {
compileInput('<input type="text" name="input" ng-model="value" minlength="{{ min }}" />');
scope.$apply(function() {
scope.min = 10;
});

changeInputValueTo('12345');
expect(inputElm).toBeInvalid();
expect(scope.form.input.$error.minlength).toBe(true);

scope.$apply(function() {
scope.min = 5;
});

expect(inputElm).toBeValid();
expect(scope.form.input.$error.minlength).not.toBe(true);
});
});


Expand Down Expand Up @@ -1396,6 +1414,24 @@ describe('input', function() {

expect(value).toBe(10);
});

it('should observe the standard maxlength attribute and register it as a validator on the model', function() {
compileInput('<input type="text" name="input" ng-model="value" maxlength="{{ max }}" />');
scope.$apply(function() {
scope.max = 1;
});

changeInputValueTo('12345');
expect(inputElm).toBeInvalid();
expect(scope.form.input.$error.maxlength).toBe(true);

scope.$apply(function() {
scope.max = 6;
});

expect(inputElm).toBeValid();
expect(scope.form.input.$error.maxlength).not.toBe(true);
});
});


Expand Down

0 comments on commit 26d91b6

Please sign in to comment.