Skip to content

Commit

Permalink
Merge branch 'nested_fields_merge' - merges #77, fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 25, 2013
2 parents 23ca146 + 45dc833 commit ac4a0ce
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
7 changes: 6 additions & 1 deletion example/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ var form = forms.create({
notes: fields.string({
widget: widgets.textarea({rows: 6})
}),
spam_me: fields.boolean()
spam_me: fields.boolean(),
nested_1: {
nested_2: {
nested: fields.string()
}
}
});


Expand Down
14 changes: 11 additions & 3 deletions example/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ var reg_form = forms.create({
required: true,
validators: [validators.matchField('password')]
}),
email: fields.email()
personal: {
name: fields.string({required: true, label: 'Name'}),
email: fields.email({required: true, label: 'Email'}),
address: {
address1: fields.string({required: true, label: 'Address 1'}),
address2: fields.string({label: 'Address 2'}),
city: fields.string({required: true, label: 'City'}),
state: fields.string({required: true, label: 'State'}),
zip: fields.number({required: true, label: 'ZIP'})
}
}
});


http.createServer(function (req, res) {
reg_form.handle(req, {
success: function (form) {
var req_data = require('url').parse(req.url, 1).query;
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Success!</h1>');
res.end('<pre>' + util.inspect(form.data) + '</pre>');
Expand Down
5 changes: 4 additions & 1 deletion lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ exports.string = function (opt) {
return f;
};


exports.number = function (opt) {
if (!opt) { opt = {}; }
var f = exports.string(opt);
Expand Down Expand Up @@ -170,3 +169,7 @@ exports.date = function (opt) {
return f;
};

exports.object = function (fields, opts) {
return forms.create(fields || {});
};

27 changes: 21 additions & 6 deletions lib/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var async = require('async'),
http = require('http'),
querystring = require('querystring'),
querystring = require('qs'),
parse = require('url').parse;


Expand All @@ -14,6 +14,10 @@ exports.validators = require('./validators');

exports.create = function (fields) {
Object.keys(fields).forEach(function (k) {
// if it's not a field object, create an object field.
if (typeof fields[k].toHTML !== 'function' && typeof fields[k] === 'object') {
fields[k] = exports.fields.object(fields[k]);
}
fields[k].name = k;
});
var f = {
Expand All @@ -23,13 +27,17 @@ exports.create = function (fields) {
b.toHTML = f.toHTML;
b.fields = {};
Object.keys(f.fields).forEach(function (k) {
b.fields[k] = f.fields[k].bind(data[k]);
b.fields[k] = data[k] ? f.fields[k].bind(data[k]) : f.fields[k].bind('');
});
b.data = Object.keys(b.fields).reduce(function (a, k) {
a[k] = b.fields[k].data;
return a;
}, {});
b.validate = function (callback) {
b.validate = function (obj, callback) {
if (typeof obj === 'function') {
callback = obj;
}

async.forEach(Object.keys(b.fields), function (k, callback) {
b.fields[k].validate(b, function (err, bound_field) {
b.fields[k] = bound_field;
Expand All @@ -52,7 +60,8 @@ exports.create = function (fields) {
(callbacks.empty || callbacks.other)(f);
} else if (obj instanceof http.IncomingMessage) {
if (obj.method === 'GET') {
f.handle(parse(obj.url, 1).query, callbacks);
var qs = parse(obj.url).query;
f.handle(querystring.parse(qs), callbacks);
} else if (obj.method === 'POST' || obj.method === 'PUT') {
// If the app is using bodyDecoder for connect or express,
// it has already put all the POST data into request.body.
Expand Down Expand Up @@ -82,10 +91,16 @@ exports.create = function (fields) {
throw new Error('Cannot handle type: ' + typeof obj);
}
},
toHTML: function (iterator) {
toHTML: function (name, iterator) {
var form = this;

if (typeof name === 'function') {
iterator = name;
}

return Object.keys(form.fields).reduce(function (html, k) {
return html + form.fields[k].toHTML(k, iterator);
var kname = typeof name === 'string' ? name+'['+k+']' : k;
return html + form.fields[k].toHTML(kname, iterator);
}, '');
}
};
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"test-browser": "./node_modules/.bin/browserify test-browser.js | ./node_modules/.bin/testling"
},
"dependencies": {
"async": "~0.2.9"
"async": "~0.2.9",
"qs": "~0.6.5"
},
"licenses": [
{
Expand Down Expand Up @@ -45,8 +46,8 @@
},
"devDependencies": {
"nodeunit": "~0.8.1",
"testling": "~1.2.2",
"browserify": "~2.25.0",
"testling": "~1.5.1",
"browserify": "~2.29.0",
"nodeunit-browser-tap": "~0.0.3"
}
}
Expand Down

0 comments on commit ac4a0ce

Please sign in to comment.