Skip to content

Commit bfcc0c8

Browse files
committed
feat(input): add handling for date input
partially closes angular#757
1 parent 043190f commit bfcc0c8

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

src/ng/directive/input.js

+140
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
1212
var EMAIL_REGEXP = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/;
1313
var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;
14+
var DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})$/;
1415

1516
var inputType = {
1617

@@ -89,6 +90,71 @@ var inputType = {
8990
*/
9091
'text': textInputType,
9192

93+
/**
94+
* @ngdoc inputType
95+
* @name ng.directive:input.date
96+
*
97+
* @description
98+
* HTML5 or text input with date validation and transformation. In browsers that do not yet support
99+
* the HTML5 date input, a text element will be used. The text must be entered in a valid ISO-8601
100+
* date format (yyyy-MM-dd), for example: `2009-01-06`. Will also accept a valid ISO date or Date object
101+
* as model input, but will always output a Date object to the model.
102+
*
103+
* @param {string} ngModel Assignable angular expression to data-bind to.
104+
* @param {string=} name Property name of the form under which the control is published.
105+
* @param {string=} min Sets the `min` validation error key if the value entered is less than `min`.
106+
* @param {string=} max Sets the `max` validation error key if the value entered is greater than `max`.
107+
* @param {string=} required Sets `required` validation error key if the value is not entered.
108+
* @param {string=} ngRequired Adds `required` attribute and `required` validation constraint to
109+
* the element when the ngRequired expression evaluates to true. Use `ngRequired` instead of
110+
* `required` when you want to data-bind to the `required` attribute.
111+
* @param {string=} ngChange Angular expression to be executed when input changes due to user
112+
* interaction with the input element.
113+
*
114+
* @example
115+
<doc:example>
116+
<doc:source>
117+
<script>
118+
function Ctrl($scope) {
119+
$scope.value = '2013-10-22';
120+
}
121+
</script>
122+
<form name="myForm" ng-controller="Ctrl as dateCtrl">
123+
Pick a date between in 2013:
124+
<input type="date" name="input" ng-model="value"
125+
placeholder="yyyy-MM-dd" min="2013-01-01" max="2013-12-31" required />
126+
<span class="error" ng-show="myForm.input.$error.required">
127+
Required!</span>
128+
<span class="error" ng-show="myForm.input.$error.date">
129+
Not a valid date!</span>
130+
<tt>value = {{value}}</tt><br/>
131+
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
132+
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
133+
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
134+
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
135+
</form>
136+
</doc:source>
137+
<doc:scenario>
138+
it('should initialize to model', function() {
139+
expect(binding('value')).toEqual('2013-10-22');
140+
expect(binding('myForm.input.$valid')).toEqual('true');
141+
});
142+
143+
it('should be invalid if empty', function() {
144+
input('value').enter('');
145+
expect(binding('value')).toEqual('');
146+
expect(binding('myForm.input.$valid')).toEqual('false');
147+
});
148+
149+
it('should be invalid if over max', function() {
150+
input('value').enter('2015-01-01');
151+
expect(binding('value')).toEqual('');
152+
expect(binding('myForm.input.$valid')).toEqual('false');
153+
});
154+
</doc:scenario>
155+
</doc:example>
156+
*/
157+
'date': dateInputType,
92158

93159
/**
94160
* @ngdoc inputType
@@ -537,6 +603,80 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
537603
}
538604
}
539605

606+
function dateInputType(scope, element, attr, ctrl, $sniffer, $browser) {
607+
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
608+
609+
ctrl.$parsers.push(function(value) {
610+
if(ctrl.$isEmpty(value)) {
611+
ctrl.$setValidity('date', true);
612+
return value;
613+
}
614+
615+
if(DATE_REGEXP.test(value)) {
616+
ctrl.$setValidity('date', true);
617+
return new Date(getTime(value));
618+
}
619+
620+
ctrl.$setValidity('date', false);
621+
return undefined;
622+
});
623+
624+
ctrl.$formatters.push(function(value) {
625+
if(isDate(value)) {
626+
var year = value.getFullYear(),
627+
month = value.getMonth() + 1,
628+
day = value.getDate();
629+
630+
month = (month < 10 ? '0' : '') + month;
631+
day = (day < 10 ? '0' : '') + day;
632+
return year + '-' + month + '-' + day;
633+
}
634+
return ctrl.$isEmpty(value) ? '' : '' + value;
635+
});
636+
637+
if(attr.min) {
638+
var minValidator = function(value) {
639+
var valid = ctrl.$isEmpty(value) ||
640+
(getTime(value) >= getTime(attr.min));
641+
ctrl.$setValidity('min', valid);
642+
return valid ? value : undefined;
643+
};
644+
645+
ctrl.$parsers.push(minValidator);
646+
ctrl.$formatters.push(minValidator);
647+
}
648+
649+
if(attr.max) {
650+
var maxValidator = function(value) {
651+
var valid = ctrl.$isEmpty(value) ||
652+
(getTime(value) <= getTime(attr.max));
653+
ctrl.$setValidity('max', valid);
654+
return valid ? value : undefined;
655+
};
656+
657+
ctrl.$parsers.push(maxValidator);
658+
ctrl.$formatters.push(maxValidator);
659+
}
660+
661+
function getTime(iso) {
662+
if(isDate(iso)) {
663+
return +iso;
664+
}
665+
666+
if(isString(iso)) {
667+
DATE_REGEXP.lastIndex = 0;
668+
var parts = DATE_REGEXP.exec(iso),
669+
yyyy = +parts[1],
670+
mm = +parts[2] - 1,
671+
dd = +parts[3],
672+
time = new Date(yyyy, mm, dd);
673+
return +time;
674+
}
675+
676+
return NaN;
677+
}
678+
}
679+
540680
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
541681
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
542682

test/ng/directive/inputSpec.js

+116
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,122 @@ describe('input', function() {
717717

718718
// INPUT TYPES
719719

720+
describe('date', function () {
721+
it('should set the view if the model is valid ISO8601 date', function() {
722+
compileInput('<input type="date" ng-model="birthday"/>');
723+
724+
scope.$apply(function(){
725+
scope.birthday = '1977-10-22';
726+
});
727+
728+
expect(inputElm.val()).toBe('1977-10-22');
729+
});
730+
731+
it('should set the view if the model if a valid Date object.', function(){
732+
compileInput('<input type="date" ng-model="christmas"/>');
733+
734+
scope.$apply(function (){
735+
scope.christmas = new Date(2013, 11, 25);
736+
});
737+
738+
expect(inputElm.val()).toBe('2013-12-25');
739+
});
740+
741+
it('should set the model undefined if the view is invalid', function (){
742+
compileInput('<input type="date" ng-model="arrMatey"/>');
743+
744+
scope.$apply(function (){
745+
scope.arrMatey = new Date(2014, 8, 14);
746+
});
747+
748+
expect(inputElm.val()).toBe('2014-09-14');
749+
750+
try {
751+
//set to text for browsers with date validation.
752+
inputElm[0].setAttribute('type', 'text');
753+
} catch(e) {
754+
//for IE8
755+
}
756+
757+
changeInputValueTo('1-2-3');
758+
expect(inputElm.val()).toBe('1-2-3');
759+
expect(scope.arrMatey).toBeUndefined();
760+
expect(inputElm).toBeInvalid();
761+
});
762+
763+
describe('min', function (){
764+
beforeEach(function (){
765+
compileInput('<input type="date" ng-model="value" name="alias" min="2000-01-01" />');
766+
scope.$digest();
767+
});
768+
769+
it('should invalidate', function (){
770+
changeInputValueTo('1999-12-31');
771+
expect(inputElm).toBeInvalid();
772+
expect(scope.value).toBeFalsy();
773+
expect(scope.form.alias.$error.min).toBeTruthy();
774+
});
775+
776+
it('should validate', function (){
777+
changeInputValueTo('2000-01-01');
778+
expect(inputElm).toBeValid();
779+
expect(+scope.value).toBe(+new Date(2000, 0, 1));
780+
expect(scope.form.alias.$error.min).toBeFalsy();
781+
});
782+
});
783+
784+
describe('max', function (){
785+
beforeEach(function (){
786+
compileInput('<input type="date" ng-model="value" name="alias" max="2019-01-01" />');
787+
scope.$digest();
788+
});
789+
790+
it('should invalidate', function (){
791+
changeInputValueTo('2019-12-31');
792+
expect(inputElm).toBeInvalid();
793+
expect(scope.value).toBeFalsy();
794+
expect(scope.form.alias.$error.max).toBeTruthy();
795+
});
796+
797+
it('should validate', function() {
798+
changeInputValueTo('2000-01-01');
799+
expect(inputElm).toBeValid();
800+
expect(+scope.value).toBe(+new Date(2000, 0, 1));
801+
expect(scope.form.alias.$error.max).toBeFalsy();
802+
});
803+
});
804+
805+
it('should validate even if max value changes on-the-fly', function(done) {
806+
scope.max = '2013-01-01';
807+
compileInput('<input type="date" ng-model="value" name="alias" max="{{max}}" />');
808+
scope.$digest();
809+
810+
changeInputValueTo('2014-01-01');
811+
expect(inputElm).toBeInvalid();
812+
813+
scope.max = '2001-01-01';
814+
scope.$digest(function () {
815+
expect(inputElm).toBeValid();
816+
done();
817+
});
818+
});
819+
820+
it('should validate even if min value changes on-the-fly', function(done) {
821+
scope.min = '2013-01-01';
822+
compileInput('<input type="date" ng-model="value" name="alias" min="{{min}}" />');
823+
scope.$digest();
824+
825+
changeInputValueTo('2010-01-01');
826+
expect(inputElm).toBeInvalid();
827+
828+
scope.min = '2014-01-01';
829+
scope.$digest(function () {
830+
expect(inputElm).toBeValid();
831+
done();
832+
});
833+
});
834+
});
835+
720836
describe('number', function() {
721837

722838
it('should reset the model if view is invalid', function() {

0 commit comments

Comments
 (0)