CarbonJS Forms / carbon-form

The carbon-form module provides easy-to-use forms in your projects. It is a complete solution which not only provides rendering but filtering and validation too. It packs all the logic behind HTML forms and it abstracts a lot of work so that you can focus on building awesome web applications and best of all it allows you to define the layout and style your forms any way you want them.

If you have ever used Zend_Form before you're going to be familiar with the syntax and if not just keep reading.

Installation

npm install carbon-form [--save]

Usage

The usage is pretty simple. You create a form and then add elements to it. For each element you define a set of options such as name, label, filters, validators, HTML attributes etc. The following example should present most of carbon-form features.

Defining form layout (file: signup-form.jade)

Since carbon-form gives you freedom to style your own forms any way you wish, you can define form layout in the separate file and then tell to carbon-form where to look for this file. When the form renders it will use this layout as a template.

Validation and rendering (using carbon-framework)

This example features carbon-framework just to make it easier for you to understand how carbon-form works in reality. Of course you can use carbon-form with any other Node.js framework or no framework at all.

module.exports = function() {
return {
signupAction: {
post: function(req, res) {
var postData = req.body;
var form = require("./forms/signup-form")({
viewiewScriptPaths: res.viewPaths
});
form.isValid(postData, function(err, values) {
if (err)
{
form.render(function(err) {
// Now in the view all you have to do is call `!= form.render()`
// (if you're using Jade engine) and it will return rendered form
// as HTML all together with all errored fields
res.render("scripts/signup", {
formSignup: form
});
});
}
else
{
// Form validation is successful and argument `values` now contains all
// field values which are filtered and validated and therefor safe
// to be inserted into the database
res.redirect("/signup-success");
}
});
}
}
}
}