-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Objects Field (nested forms) #77
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
var async = require('async'), | ||
http = require('http'), | ||
querystring = require('querystring'), | ||
querystring = require('qs'), | ||
parse = require('url').parse; | ||
|
||
|
||
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please always use "===" and never "==" - also, please always put a space between "if" and the opening paren. |
||
fields[k] = exports.fields.object(fields[k]); | ||
} | ||
fields[k].name = k; | ||
}); | ||
var f = { | ||
|
@@ -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(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the unnecessary parens around the ternary condition, and please ensure there is always a single space surrounding the ? and : in a ternary. |
||
}); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fundamentally, the approach is emulate the field methods. When a nested form is validated, it is treated as a field and expects it to handle two arguments: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is useful. If we remove the first argument, it will break. If we need the form object in the future, it's there. I'm open to alternative approaches, but I think there's a certain elegance to this solution. |
||
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; | ||
|
@@ -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. | ||
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the polymorphic signatures? if name is optional, it should always come last, and if there's more than one optional arg, please pass it in an options hash. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The form object treats nested forms as fields, so I'm following the same convention as the fields. |
||
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); | ||
}, ''); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please always add trailing newlines to files