GithubHelp home page GithubHelp logo

connect-iform's Introduction

node-iform

node-iform is a connect middleware help you validate and convert form data.

NOTE You need to view node-validator for more information.

Install

npm install iform

Example

    var iform = require('iform');

    var userForm = iform({
            username: {
                required : true,
                len : [4, 15]
            },
            password: {
                required : true,
                len : [6, 20]
            },

            email : {
                type : 'email'
            },

            birth : {
                type : Date,
                isAfter: new Date('01/01/1900'),
                isBefore : null // means now
            },

            avatar : {
                defaultValue : function(req) {
                    return '/avatar/' + req.body.username + '.png';
                }
            },

            age : 'int',

            blog : 'url'
    });

    app.post('/signup', userForm(), function(req, res, next) {
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.insert(req.iform.data, function(err, data) {
                res.json({success : true, message: 'Sign up successfully'});
            });
    });

    app.post('/profile', userForm('birth', 'age', 'blog'), function(req, res, next){
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.update({username : req.session.user.username}, req.iform.data, function(err, data) {
                res.json({success : true, message: 'Update profile successfully'});
            });
    });

define rules

At first you need define some rules for validation

As you can see in the example, define a form like this : var form = iform(rules);

rules is like {fieldName : fieldRules, ...}

fieldRules is like {ruleName : ruleParameter, ...}

        // field name | rule name | rule parameters
           username  :{ len       : [4, 15] }

The rule names can find at node-validator project page.

All the methods of Validator and Filter of node-validator can be use as a rule name. The rule parameters is the arguments for that method.

The len is defined by node-validator like this

    Validator.prototype.len = function(min, max) { ... }

It takes two parameters. so we use an array as the parameters.

The type is a special rule ,e.g.

    email : {
        type : 'email'
    }

it is equals to

    email : {
        'isEmail' : []
    }

you can also use int, date etc, cause the Validator defined isInt and isDate

all the method of Valiator starts with is and take no arguments can be use as a type.

if you only have a type rule you can use fieldName : type define it.

You can also use Date Number instead of 'date', 'number'

use the middleware

userForm you just defined is a function which returns a middleware, use like this

    app.post('/signup', userForm(), function(req, res, next) {
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.insert(req.iform.data, function(err, data) {
                res.json({success : true, message: 'Sign up successfully'});
            });
    });

the middleware will check the req.body by your rules, all the validation errors go to req.iform.errors, and the filtered and converted data go to req.iform.data.

Since the data has been cleaned, you can use it immediately.

If there is another page also use the smae rules but only part of fields, you can reuse it like this.

    app.post('/profile', userForm('birth', 'age', 'blog'), function(req, res, next){
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.update({username : req.session.user.username}, req.iform.data, function(err, data) {
                res.json({success : true, message: 'Update profile successfully'});
            });
    });

connect-iform's People

Contributors

guileen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

connect-iform's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.