castform
Form validation on the client and server.
usage
server.js
Castform can work with the a server instance and can also be a middleware for connect, express, and flatiron. See the examples folder for working examples.
var castform = require('castform');
var server = http.createServer();
castform(options, server);
connect()
.use(castform(options));
The options
object given to castform will contain the schemas on how your forms are validated. See the example in example/options.js
.
The top level must contain a forms
object, which must contain a fields
object with the names of the fields as they key and an object containing options for it.
The following are the available options:
{
validate: [
{ fn: function checkLength(s) { return s.length < 15; }
msg: 'Must be between 1 and 15 characters' },
{ fn: /^[a-zA-Z0-9_]*$/
msg: 'Must contain only the characters a-zA-Z0-9_' },
{ fn: function isAvailable(value, done) {
db.collection.findOne(value, function(err, doc) {
if (err) {
done(false, err.message);
} else if (doc) {
done(false);
} else {
done(true);
}
});
}
msg: 'That username is taken' },
],
required: true,
storage: {
session: false,
force: false,
cache: false,
},
cache: {},
delay: 500,
sanitize: function(value) { return parseInt(value, 10); }
load: function($field, options) {
},
check: function($field, options) {
},
pass: function($field, options, pageLoad, success, validationOptions) {
},
submit: {
server: function(values, pass) {
db.users.add(values, function(err) {
pass(!err, err ? err.message : null);
});
},
client: {
before: function($submit, options, values) {
},
validate: function($submit, options, values, pass) {
}
pass: function($submit, options, values, success, message) {
}
}
},
styles: {
field: 'castform-field',
success: 'castform-success',
fail: 'castform-fail',
submit: 'castform-submit',
tooltip: 'castform-tooltip'
}
}
client side
On the client, you'll need to include the script that castform provides in /castform/castform.js
.
<script src="/castform/castform.js"></script>
Castform should find your forms by looking at their id.
<form id="form-signup">
<div>
<label for="username">username</label>
<input name="username" id="username" />
</div>
<div>
<input type="submit" />
</div>
<form>
Options can also be accessed and set on the client side with castform.options
.
<script src="/castform/castform.js"></script>
<script type="text/javascript">
castform.options.forms.signup.myNewField = {
required: true,
};
</script>
install
$ npm install castform
tests
Tests are written with nodeunit.
$ npm test