npm install --save connect-forms-validator
var formMiddleware = require('connect-forms-validator');
someForm = formMiddleware(
form.field("username").required()
);
app.any('/login', someForm, function(req, res){
if (req.form.isValid()){
// process req.form.data
}
res.render('login.html');
});
The form middleware is the main entrypoint to the connect-forms-validator package.
var formMiddleware = require('connect-forms-validator');
The form middleware accepts a variable number of fields as a constructor
Mount middleware to this form like express.Router
.
Unlike the Router formMiddleware.use
only takes one callback argument.
Mount middleware to this form. The success
middleware will only be called when a non GET,HEAD,OPTIONS
method is used and all of the field validators pass.
Flash an error message if any of the fields are invalid.
Form objects are set on the req oject by the form middleware.
req.form
The modified field data that has been sanitied by field middleware
The unmodified field data
An object that contains {fieldname: [errorMessage, ... ]}
entries
Returns true if form.errors is not empty
Returns true if form.errors
is not empty
The Field
object has a middleware stack and can 'use' middleware itself
Fields can be accessed from the main form template
var form = require('connect-forms-validator');
var field = form.field;
var form.field("username").trim().use(function(req, res, next, keyname, value){
})
A function with the signature callback(req, res, next, key, value)
If next is called with a string,
the middleware processing will stop for this field and form.addError(key, message)
will be called.
Ensure the field is a boolean value
Call string trim method on the value
Flag an error if the field is not given
Flash error if the field has an error
Match the field value against a regular expression
Set the default value for this field
Restrict this field to an array of choices