Skip to content
Browse files

Adding a "date" field, widget, and validator. Fixes #53.

  • Loading branch information...
1 parent eb93b35 commit cc7abd895246ac81ccf4044ff61db837d2c9f87c @ljharb ljharb committed Feb 22, 2013
Showing with 89 additions and 0 deletions.
  1. +3 −0 README.md
  2. +11 −0 lib/fields.js
  3. +15 −0 lib/validators.js
  4. +1 −0 lib/widgets.js
  5. +34 −0 test/test-fields.js
  6. +24 −0 test/test-validators.js
  7. +1 −0 test/test-widgets.js
View
3 README.md
@@ -111,13 +111,15 @@ components following the same API.
* password
* email
* url
+* date
### Widgets
* text
* password
* hidden
* color
+* date
* checkbox
* select
* textarea
@@ -139,6 +141,7 @@ components following the same API.
* color
* email
* url
+* date
### Renderers
View
11 lib/fields.js
@@ -148,3 +148,14 @@ exports.array = function (opt) {
return f;
};
+exports.date = function (opt) {
+ if (!opt) { opt = {}; }
+ var f = exports.string(opt);
+ if (f.validators) {
+ f.validators.unshift(forms.validators.date());
+ } else {
+ f.validators = [forms.validators.date()];
+ }
+ return f;
+};
+
View
15 lib/validators.js
@@ -108,3 +108,18 @@ exports.url = function (include_localhost) {
return exports.regexp(include_localhost ? with_localhost_regex : external_regex, 'Please enter a valid URL.');
};
+exports.date = function () {
+ var numberRegex = /^[0-9]+$/,
+ invalidDate = new Date('z');
+ return function (form, field, callback) {
+ var parts = field.data ? field.data.split('-') : [],
+ allNumbers = parts.every(function (part) { return numberRegex.test(part); }),
+ date = allNumbers && parts.length === 3 ? new Date(parts[0], parts[1] - 1, parts[2]) : invalidDate;
+ if (!isNaN(date.getTime())) {
+ callback();
+ } else {
+ callback('Inputs of type "date" must be valid dates in the format "yyyy-mm-dd"');
+ }
+ };
+};
+
View
1 lib/widgets.js
@@ -88,6 +88,7 @@ exports.text = input('text');
exports.password = input('password');
exports.hidden = input('hidden');
exports.color = input('color');
+exports.date = input('date');
exports.checkbox = function (opt) {
if (!opt) { opt = {}; }
View
34 test/test-fields.js
@@ -335,6 +335,40 @@ exports['url validators'] = function (test) {
test.done();
};
+testField('date');
+
+exports['date parse'] = function (test) {
+ test.equals(
+ fields.date().parse.toString(),
+ fields.string().parse.toString()
+ );
+ test.done();
+};
+
+exports['date toHTML'] = function (test) {
+ test.equals(
+ fields.date().toHTML.toString(),
+ fields.string().toHTML.toString()
+ );
+ test.done();
+};
+
+exports['date validators'] = function (test) {
+ test.equals(
+ fields.date().validators[0].toString(),
+ forms.validators.date().toString()
+ );
+ var fn1 = function () { return 'one'; },
+ fn2 = function () { return 'two'; },
+ f = fields.date({validators: [fn1, fn2]});
+ test.equals(
+ f.validators[0].toString(),
+ forms.validators.date().toString()
+ );
+ test.same(f.validators.slice(1), [fn1, fn2]);
+ test.done();
+};
+
testField('array');
exports['array parse'] = function (test) {
View
24 test/test-validators.js
@@ -101,6 +101,30 @@ exports.url = function (test) {
], test.done);
};
+exports.date = function (test) {
+ var msg = 'Inputs of type "date" must be valid dates in the format "yyyy-mm-dd"';
+ async.parallel([
+ function (callback) {
+ validators.date()('form', {data: '02/28/2012'}, function (err) {
+ test.equals(err, msg);
+ validators.date()('form', {data: '2012-02-28'}, function (err) {
+ test.equals(err, undefined);
+ callback();
+ });
+ });
+ },
+ function (callback) {
+ validators.date(true)('form', {data: '2012.02.30'}, function (err) {
+ test.equals(err, msg);
+ validators.date(true)('form', {data: '2012-02-30'}, function (err) {
+ test.equals(err, undefined);
+ callback();
+ });
+ });
+ }
+ ], test.done);
+};
+
exports.minlength = function (test) {
validators.minlength(5)('form', {data: '1234'}, function (err) {
test.equals(err, 'Please enter at least 5 characters.');
View
1 test/test-widgets.js
@@ -26,6 +26,7 @@ exports.text = test_input('text');
exports.password = test_input('password');
exports.hidden = test_input('hidden');
exports.color = test_input('color');
+exports.date = test_input('date');
exports.checkbox = function (test) {
test.equals(

0 comments on commit cc7abd8

Please sign in to comment.
Something went wrong with that request. Please try again.