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

Commit c211e7a

Browse files
tobyeeNarretz
authored andcommitted
fix(input): create max and/or min validator regardless of initial value
Also adds corresponding tests for ngMin / ngMax. Fixes #10307 Closes #10327
1 parent d6eba21 commit c211e7a

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12501250
return value;
12511251
});
12521252

1253-
if (attr.min || attr.ngMin) {
1253+
if (isDefined(attr.min) || attr.ngMin) {
12541254
var minVal;
12551255
ctrl.$validators.min = function(value) {
12561256
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
@@ -1266,7 +1266,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12661266
});
12671267
}
12681268

1269-
if (attr.max || attr.ngMax) {
1269+
if (isDefined(attr.max) || attr.ngMax) {
12701270
var maxVal;
12711271
ctrl.$validators.max = function(value) {
12721272
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;

test/ng/directive/inputSpec.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -1869,12 +1869,17 @@ describe('input', function() {
18691869
});
18701870

18711871
it('should validate even if min value changes on-the-fly', function() {
1872-
$rootScope.min = 10;
1872+
$rootScope.min = undefined;
18731873
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" min="{{min}}" />');
1874+
expect(inputElm).toBeValid();
18741875

18751876
helper.changeInputValueTo('15');
18761877
expect(inputElm).toBeValid();
18771878

1879+
$rootScope.min = 10;
1880+
$rootScope.$digest();
1881+
expect(inputElm).toBeValid();
1882+
18781883
$rootScope.min = 20;
18791884
$rootScope.$digest();
18801885
expect(inputElm).toBeInvalid();
@@ -1910,12 +1915,17 @@ describe('input', function() {
19101915
});
19111916

19121917
it('should validate even if the ngMin value changes on-the-fly', function() {
1913-
$rootScope.min = 10;
1918+
$rootScope.min = undefined;
19141919
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" ng-min="min" />');
1920+
expect(inputElm).toBeValid();
19151921

19161922
helper.changeInputValueTo('15');
19171923
expect(inputElm).toBeValid();
19181924

1925+
$rootScope.min = 10;
1926+
$rootScope.$digest();
1927+
expect(inputElm).toBeValid();
1928+
19191929
$rootScope.min = 20;
19201930
$rootScope.$digest();
19211931
expect(inputElm).toBeInvalid();
@@ -1952,12 +1962,17 @@ describe('input', function() {
19521962
});
19531963

19541964
it('should validate even if max value changes on-the-fly', function() {
1955-
$rootScope.max = 10;
1965+
$rootScope.max = undefined;
19561966
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" max="{{max}}" />');
1967+
expect(inputElm).toBeValid();
19571968

19581969
helper.changeInputValueTo('5');
19591970
expect(inputElm).toBeValid();
19601971

1972+
$rootScope.max = 10;
1973+
$rootScope.$digest();
1974+
expect(inputElm).toBeValid();
1975+
19611976
$rootScope.max = 0;
19621977
$rootScope.$digest();
19631978
expect(inputElm).toBeInvalid();
@@ -1993,12 +2008,17 @@ describe('input', function() {
19932008
});
19942009

19952010
it('should validate even if the ngMax value changes on-the-fly', function() {
1996-
$rootScope.max = 10;
2011+
$rootScope.max = undefined;
19972012
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" ng-max="max" />');
2013+
expect(inputElm).toBeValid();
19982014

19992015
helper.changeInputValueTo('5');
20002016
expect(inputElm).toBeValid();
20012017

2018+
$rootScope.max = 10;
2019+
$rootScope.$digest();
2020+
expect(inputElm).toBeValid();
2021+
20022022
$rootScope.max = 0;
20032023
$rootScope.$digest();
20042024
expect(inputElm).toBeInvalid();

0 commit comments

Comments
 (0)