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

fix(input): ignore min/max if they are empty on all input types #12785

Closed
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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