simple-form
v1.0.0
Published
An easy way to create, parse, and validate forms
Downloads
11
Maintainers
Readme
simple-form
$ version 1.0.0
Form processor for filter and validation form data. This base on form. I develop this module because the idea of this package is very good, but the current module didn't work.
Installation
Install simple-form then run the following.
$ npm install simple-form --save-dev
Example
Processing an example form:
var form = require('simple-form');
var elements = {
name: [
form.filter(form.Filter.trim)
]
};
form
.create(elements)
.process({'name': ' tester ', 'active': '1'}, function(error, data) {
console.log('error:', error);
console.log('data:', data);
});
The result:
error: null
data: { name: 'tester' }
Extend your validator
You can define your own validator:
var form = require('simple-form');
form.extend('isNotEmpty', function(str) {
return (str.length > 0) ? true : false;
});
var elements = {
name: [
form.filter(form.Filter.toString),
form.filter(form.Filter.trim),
form.validator(form.Validator.isNotEmpty, 'Bad Name'),
],
email: [
form.filter(form.Filter.toString),
form.filter(form.Filter.trim),
form.validator(form.Validator.isNotEmpty, 'Bad Email'),
]
};
form
.create(elements)
.process({
active : '1',
name : ' tester ',
email : ' '
}, function(error, data) {
console.log('error:', error);
console.log('data:', data);
});
The result:
error: { email: [ 'Bad Email' ] }
data: { name: 'tester', email: '' }
Your own validator
You can use your own validator:
var form = require('simple-form');
function customNameValidator(data, name, callback) {
setTimeout(function(){
callback('So bad', data);
}, 500);
}
var elements = {
name: [
form.filter(form.Filter.toString),
form.filter(form.Filter.trim),
customNameValidator
]
};
form
.create(elements)
.process({
active : '1',
name : ' tester '
}, function(error, data) {
console.log('error:', error);
console.log('data:', data);
});
The result:
error: { name: [ 'So bad' ] }
data: { name: 'tester' }