torti
Form generator that can be validated, sanitized and extended for node.js
Install
npm install torti
API
API documentation is here
Getting started
Field
1.Define your fields, with your own rules
var myFields = ;
- The name property is necessary to be able to retrieve your field
- You can add your own properties in the options as shown in the Field password
- By default, each field is required. If you want to make it optional, add the optional validator
It can also be used as Field('fieldname'), which returns a special function. It is basically a way to delay the evaluation of the field value since it has none for now.
Form
2.var myForm = ;
- The fields property should be an array of Field
- You can also put whatever property you want in the form options. For example, I choose to set a method and an action property, but these are completely independant from the API.
When you want to validate your data, pass it to the form:
var data = email: 'foo@bar.com' password: '123456' password2: '123456';myForm;
FieldValidators
3.FieldValidators is a simple object with a string key matching the validator name and its corresponding asynchroneous function. For now, this object is global and cannot be defined for each Form, however you can add your own global validators using addValidator.
You can also add your own validator, here are some examples:
Form; Form; var myFields = ;
As you may have noticed, validators are passed:
- A callback to send back a boolean indicating the validation result as well as the modified value if necessary
- The value
- Optional arguments that are passed during the validator call (see startsWith('H'))
Sanitizers are almost the same, except that you can update the value:
Form;
No error was passed for this sanitizer since it just modifies the value, it does not need to check the value consistency here.
But you can also create a validator that actually modifies the value:
Form;
Since validators are asynchroneous, you can imagine doing database validation:
Form;
4. Predefined fields
You can also use pre-defined fields:
- EmailField: EmailField(options) ⇒ Field(options).trim().isEmail().normalizeEmail()
5. Promises
If you don't provide a callback to validate functions, it returns a promise:
myForm ;
Take a look at bluebird if you're not familiar with promises.
6. Rendering
The form is rendered as a javascript object using the render method of the Form class.
Why not HTML?
Because I think HTML has nothing to do with this part of the code, and displaying a form is up to you. You may want to display your forms differently according to the page or use JSON. This allows to customize the way you want to actually render it in your templates.
The form object contains:
- All the properties you passed in the Form constructor.
- A property errors, which is an array of global errors.
- A property valid, which is a simple boolean saying if the form is valid or not.
- A property fields which is an array of field objects.
A field object contains:
- All the properties you passed in the Field constructor.
- A property name, which is the name of the field
- A property value, which is the value of the field or an empty string
- A property errors, which is an array of validation errors
var form = ; console;/*{ hello: 'World', fields: [ { name: 'email', foo: 'bar', value: '', errors: [] }, { name: 'username', bar: 'foo', value: '', errors: [] } ], valid: true, errors: []}*/ console;/*{ hello: 'World', fields: [ { name: 'email', foo: 'notbar', value: 'shaoner@nomail.com', errors: [] }, { name: 'username', bar: 'foo', value: '', errors: [] } ], valid: true, errors: []}*/ form;
7. Customizing errors
Errors are mapped in a simple object which keys are the validator names and values are either a string or a function that returns a string.
You can customize errors that way:
var Form = ; // Override some of the error messagesFormErrorsisEmail = 'Wrong email address';FormErrors { if version === 6 return 'Wrong IPv6 address'; return 'Wrong IP address';}; // Override all using lodashFormErrors = _
Some examples
Example 1: Simple
Here we define a simple form with some fields that contain validators and sanity functions.
var Form = ;var Field = FormField; var signupForm = ; console;/* { method: 'POST', action: '/signup', foo: 'bar', fields: [ { name: 'email', value: '', errors: [] }, { name: 'password', value: '', errors: [] }, { name: 'password2', value: '', errors: [] }, { name: 'username', value: '', errors: [] } ], valid: true }*/ var body = email: ' foo@bar.com ' password: '123456' password2: '123456' username: 'Chuck_Norris'; signupForm;
Example 2: With express
router; router;
Express and dust template engine
Example 3: Signup/signin withThe partials
First you define a form partial matching our render object.
partials/form.dust
{#form} {#fields} {>"partials/form-fields/{partial}"/} {#errors} {.} {/errors} {/fields} {/form}
In partials/form there are html components for each field, here is an example:
partials/form-fields/email.dust
Email:
Then, a template for each page:
signup.dust
Signup! {>"partials/form" submit="Create your account!"/}
signin.dust
Signin! {>"partials/form" submit="Log in"/}
Routes
Finally, you define the forms and the routes:
controllers/index.js
var Form = ;var Field = FormField; var signupForm = ; var signinForm = ; // Signup routerouter ; // Signin routerouter ;
See how easy and clean? Notice that with asynchroneous validators, you could check email / username is not already taken.
Unit tests
npm test
Contributing
Contributions are most welcome, so feel free to fork and improve.