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

fix(input): create max and/or min validation whatever the initial value is #10327

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
return value;
});

if (attr.min || attr.ngMin) {
if (isDefined(attr.min) || attr.ngMin) {
var minVal;
ctrl.$validators.min = function(value) {
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
Expand All @@ -1246,7 +1246,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});
}

if (attr.max || attr.ngMax) {
if (isDefined(attr.max) || attr.ngMax) {
var maxVal;
ctrl.$validators.max = function(value) {
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;
Expand Down
28 changes: 24 additions & 4 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4211,12 +4211,17 @@ describe('input', function() {
});

it('should validate even if min value changes on-the-fly', function() {
scope.min = 10;
scope.min = undefined;
compileInput('<input type="number" ng-model="value" name="alias" min="{{min}}" />');
expect(inputElm).toBeValid();

changeInputValueTo('15');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an expect here (to all the changed tests)? We should make sure that the validator does the right thing when the min/max value is undefined and the input value is changed for the first time

expect(inputElm).toBeValid();

scope.min = 10;
scope.$digest();
expect(inputElm).toBeValid();

scope.min = 20;
scope.$digest();
expect(inputElm).toBeInvalid();
Expand Down Expand Up @@ -4252,12 +4257,17 @@ describe('input', function() {
});

it('should validate even if the ngMin value changes on-the-fly', function() {
scope.min = 10;
scope.min = undefined;
compileInput('<input type="number" ng-model="value" name="alias" ng-min="min" />');
expect(inputElm).toBeValid();

changeInputValueTo('15');
expect(inputElm).toBeValid();

scope.min = 10;
scope.$digest();
expect(inputElm).toBeValid();

scope.min = 20;
scope.$digest();
expect(inputElm).toBeInvalid();
Expand Down Expand Up @@ -4294,12 +4304,17 @@ describe('input', function() {
});

it('should validate even if max value changes on-the-fly', function() {
scope.max = 10;
scope.max = undefined;
compileInput('<input type="number" ng-model="value" name="alias" max="{{max}}" />');
expect(inputElm).toBeValid();

changeInputValueTo('5');
expect(inputElm).toBeValid();

scope.max = 10;
scope.$digest();
expect(inputElm).toBeValid();

scope.max = 0;
scope.$digest();
expect(inputElm).toBeInvalid();
Expand Down Expand Up @@ -4335,12 +4350,17 @@ describe('input', function() {
});

it('should validate even if the ngMax value changes on-the-fly', function() {
scope.max = 10;
scope.max = undefined;
compileInput('<input type="number" ng-model="value" name="alias" ng-max="max" />');
expect(inputElm).toBeValid();

changeInputValueTo('5');
expect(inputElm).toBeValid();

scope.max = 10;
scope.$digest();
expect(inputElm).toBeValid();

scope.max = 0;
scope.$digest();
expect(inputElm).toBeInvalid();
Expand Down