Skip to content
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

Changes to caolan/forms #14

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Expand Up @@ -70,6 +70,38 @@ Would produce:
You'll notice you have to provide your own form tags and submit button, its
more flexible this way ;)

If you would still like to add a form tag or a submit button, do it like so:

var forms = require('forms'),
fields = forms.fields,
widgets = forms.widgets,
validators = forms.validators;

var reg_form = forms.create({
username: fields.string({required: true}),
password: fields.password({required: true}),
confirm: fields.password({
required: true,
validators: [validators.matchField('password')]
}),
email: fields.email(),
login: fields.button({
label: 'Login',
widget: widgets.button({submit: true})
})
});

reg_form.method('post');

This would add:

<form method="post">
...
<div class="field">
<input type="submit" name="submit" id="id_submit" value="Login" />
</div>
</form>

Handling a request:

function myView(req, res){
Expand Down
8 changes: 7 additions & 1 deletion lib/fields.js
Expand Up @@ -50,7 +50,7 @@ exports.string = function(opt){
f.labelText = function(name){
var label = this.label;
if(!label){
label = name[0].toUpperCase() + name.substr(1).replace('_', ' ');
label = name[0].toUpperCase() + name.substr(1).replace(/_/g, ' ');
}
return label;
};
Expand Down Expand Up @@ -139,3 +139,9 @@ exports.array = function(opt){
};
return f;
};

exports.button = function(opt){
opt = opt || {};
var f = exports.string(opt);
return f;
};
18 changes: 17 additions & 1 deletion lib/forms.js
Expand Up @@ -12,6 +12,12 @@ exports.validators = require('./validators');
exports.create = function(fields){
var f = {
fields: fields,
method: function(method){
f.method = method;
},
action: function(url){
f.action = url;
},
bind: function(data){
var b = {};
b.toHTML = f.toHTML;
Expand Down Expand Up @@ -82,9 +88,19 @@ exports.create = function(fields){
},
toHTML: function(iterator){
var form = this;
return Object.keys(form.fields).reduce(function(html, k){
output = Object.keys(form.fields).reduce(function(html, k){
return html + form.fields[k].toHTML(k, iterator);
}, '');
// Add form tag if method is configured
var tags = '';
if (typeof form.method == 'string') {
tags = tags + ' method="' + form.method + '"';
}
if (typeof form.action == 'string') {
tags = tags + ' action="' + form.action + '"';
}
output = '<form' + tags + '>' + output + '</form>';
return output;
}
};
return f;
Expand Down
5 changes: 4 additions & 1 deletion lib/render.js
Expand Up @@ -2,7 +2,7 @@ var wrapWith = function(tag){
return function(name, field){
var label = this.label;
if(!label){
label = name[0].toUpperCase() + name.substr(1).replace('_', ' ');
label = name[0].toUpperCase() + name.substr(1).replace(/_/g, ' ');
}
var html = '<' + tag + ' class="' + field.classes().join(' ') + '">';
if(field.widget.type == 'multipleCheckbox' ||
Expand All @@ -13,6 +13,9 @@ var wrapWith = function(tag){
field.widget.toHTML(name, field) +
'</fieldset>';
}
else if (field.widget.type == 'button') {
html += field.widget.toHTML(name, field);
}
else {
html += field.errorHTML() +
field.labelHTML(name, field.id) +
Expand Down
15 changes: 15 additions & 0 deletions lib/widgets.js
Expand Up @@ -193,3 +193,18 @@ exports.multipleSelect = function(opt){
}
return w;
};

exports.button = function(opt){
var opt = opt || {};
var w = {};
w.classes = opt.classes || [];
w.type = 'button';
w.toHTML = function(name, f){
var f = f || {};
var html = opt.submit ? '<input type="submit"' : '<input type="button"';
html += f.label ? ' value="' + f.label + '"' : ' value="Submit"';
html += attrs({name: name, id: f.id, classes: w.classes});
return html + ' />';
}
return w;
};