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

Commit

Permalink
fix(input): ignore min/max if they are empty on all input types
Browse files Browse the repository at this point in the history
When the min/max attributes are empty (i.e. `attrs.min/max === ''`), there
should be no min/max validation applied (i.e. all values should be valid
wrt min/max). This works correctly for `input[number]`, but not for date
input family (`input[date/datetime-local/time/week/month]`).

In the case on `input[number]`, an empty string for `attrs.min/max` is
translated to `undefined` for `minVal/maxVal` and a check for
`isUndefined(minVal/maxVal)` ensures that no min/max validation takes
place.
For the data input family, an empty string for `attrs.min/max` is
translated to `NaN` (by `parseDate()`), so an additional check (for
`isNaN(minVal/maxVal)`) is required.

Fixes #12363

Closes #12785
  • Loading branch information
gkalpak committed Sep 9, 2015
1 parent 7175d0d commit 544001f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ng/directive/input.js
Expand Up @@ -1326,7 +1326,7 @@ function createDateInputType(type, regexp, parseDate, format) {
}

function parseObservedDateValue(val) {
return isDefined(val) ? (isDate(val) ? val : parseDate(val)) : undefined;
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
}
};
}
Expand Down
91 changes: 88 additions & 3 deletions test/ng/directive/inputSpec.js
Expand Up @@ -688,6 +688,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.min).toBeTruthy();
});

it('should validate if min is empty', function() {
$rootScope.minVal = undefined;
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
$rootScope.$digest();

expect($rootScope.form.alias.$error.min).toBeFalsy();
});
});

describe('max', function() {
Expand Down Expand Up @@ -722,6 +730,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.max).toBeTruthy();
});

it('should validate if max is empty', function() {
$rootScope.maxVal = undefined;
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
});
});
});

Expand Down Expand Up @@ -886,6 +902,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.min).toBeTruthy();
});

it('should validate if min is empty', function() {
$rootScope.minVal = undefined;
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
$rootScope.$digest();

expect($rootScope.form.alias.$error.min).toBeFalsy();
});
});

describe('max', function() {
Expand Down Expand Up @@ -921,6 +945,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.max).toBeTruthy();
});

it('should validate if max is empty', function() {
$rootScope.maxVal = undefined;
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
});
});
});

Expand Down Expand Up @@ -1119,6 +1151,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.min).toBeTruthy();
});

it('should validate if min is empty', function() {
$rootScope.minVal = undefined;
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
$rootScope.$digest();

expect($rootScope.form.alias.$error.min).toBeFalsy();
});
});

describe('max', function() {
Expand Down Expand Up @@ -1153,6 +1193,14 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.max).toBeTruthy();
});

it('should validate if max is empty', function() {
$rootScope.maxVal = undefined;
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
});
});


Expand Down Expand Up @@ -1428,12 +1476,21 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
expect($rootScope.form.alias.$error.min).toBeTruthy();
});

it('should validate if min is empty', function() {
$rootScope.minVal = undefined;
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
$rootScope.$digest();

expect($rootScope.form.alias.$error.min).toBeFalsy();
});
});

describe('max', function() {
var inputElm;
beforeEach(function() {
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="22:30:00" />');
$rootScope.maxVal = '22:30:00';
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="{{ maxVal }}" />');
});

it('should invalidate', function() {
Expand All @@ -1449,11 +1506,19 @@ describe('input', function() {
expect(+$rootScope.value).toBe(+new Date(1970, 0, 1, 5, 30, 0));
expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate if max is empty', function() {
$rootScope.maxVal = undefined;
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
});
});


it('should validate even if max value changes on-the-fly', function() {
$rootScope.max = '4:02:00';
$rootScope.max = '04:02:00';
var inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="{{max}}" />');

helper.changeInputValueTo('05:34:00');
Expand Down Expand Up @@ -1481,7 +1546,7 @@ describe('input', function() {


it('should validate even if ng-max value changes on-the-fly', function() {
$rootScope.max = '4:02:00';
$rootScope.max = '04:02:00';
var inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" ng-max="max" />');

helper.changeInputValueTo('05:34:00');
Expand Down Expand Up @@ -1706,6 +1771,16 @@ describe('input', function() {

expect($rootScope.form.myControl.$error.min).toBeTruthy();
});

it('should validate if min is empty', function() {
var inputElm = helper.compileInput(
'<input type="date" name="alias" ng-model="value" min />');

$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
$rootScope.$digest();

expect($rootScope.form.alias.$error.min).toBeFalsy();
});
});

describe('max', function() {
Expand Down Expand Up @@ -1735,6 +1810,16 @@ describe('input', function() {

expect($rootScope.form.myControl.$error.max).toBeTruthy();
});

it('should validate if max is empty', function() {
var inputElm = helper.compileInput(
'<input type="date" name="alias" ng-model="value" max />');

$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
});
});


Expand Down

0 comments on commit 544001f

Please sign in to comment.