Skip to content

Commit

Permalink
Adding a "date" field, widget, and validator. Fixes #53.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 22, 2013
1 parent eb93b35 commit cc7abd8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -111,13 +111,15 @@ components following the same API.
* password * password
* email * email
* url * url
* date


### Widgets ### Widgets


* text * text
* password * password
* hidden * hidden
* color * color
* date
* checkbox * checkbox
* select * select
* textarea * textarea
Expand All @@ -139,6 +141,7 @@ components following the same API.
* color * color
* email * email
* url * url
* date


### Renderers ### Renderers


Expand Down
11 changes: 11 additions & 0 deletions lib/fields.js
Expand Up @@ -148,3 +148,14 @@ exports.array = function (opt) {
return f; 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;
};

15 changes: 15 additions & 0 deletions lib/validators.js
Expand Up @@ -108,3 +108,18 @@ exports.url = function (include_localhost) {
return exports.regexp(include_localhost ? with_localhost_regex : external_regex, 'Please enter a valid URL.'); 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"');
}
};
};

1 change: 1 addition & 0 deletions lib/widgets.js
Expand Up @@ -88,6 +88,7 @@ exports.text = input('text');
exports.password = input('password'); exports.password = input('password');
exports.hidden = input('hidden'); exports.hidden = input('hidden');
exports.color = input('color'); exports.color = input('color');
exports.date = input('date');


exports.checkbox = function (opt) { exports.checkbox = function (opt) {
if (!opt) { opt = {}; } if (!opt) { opt = {}; }
Expand Down
34 changes: 34 additions & 0 deletions test/test-fields.js
Expand Up @@ -335,6 +335,40 @@ exports['url validators'] = function (test) {
test.done(); 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'); testField('array');


exports['array parse'] = function (test) { exports['array parse'] = function (test) {
Expand Down
24 changes: 24 additions & 0 deletions test/test-validators.js
Expand Up @@ -101,6 +101,30 @@ exports.url = function (test) {
], test.done); ], 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) { exports.minlength = function (test) {
validators.minlength(5)('form', {data: '1234'}, function (err) { validators.minlength(5)('form', {data: '1234'}, function (err) {
test.equals(err, 'Please enter at least 5 characters.'); test.equals(err, 'Please enter at least 5 characters.');
Expand Down
1 change: 1 addition & 0 deletions test/test-widgets.js
Expand Up @@ -26,6 +26,7 @@ exports.text = test_input('text');
exports.password = test_input('password'); exports.password = test_input('password');
exports.hidden = test_input('hidden'); exports.hidden = test_input('hidden');
exports.color = test_input('color'); exports.color = test_input('color');
exports.date = test_input('date');


exports.checkbox = function (test) { exports.checkbox = function (test) {
test.equals( test.equals(
Expand Down

0 comments on commit cc7abd8

Please sign in to comment.