Name: node-connect-forms-validator
Owner: Continuum Analytics, Inc.
Description: NodeJS Forms middleware
Created: 2015-03-30 20:41:32.0
Updated: 2017-09-17 19:30:25.0
Pushed: 2015-04-06 01:16:58.0
Homepage: null
Size: 144
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
install --save connect-forms-validator
formMiddleware = require('connect-forms-validator');
Form = formMiddleware(
rm.field("username").required()
any('/login', someForm, function(req, res){
(req.form.isValid()){
// process req.form.data
s.render('login.html');
The form middleware is the main entrypoint to the connect-forms-validator package.
formMiddleware = require('connect-forms-validator');
formMiddleware(field, [, fields, ...])
The form middleware accepts a variable number of fields as a constructor
formMiddleware.use(callback)
Mount middleware to this form like express.Router
.
Unlike the Router formMiddleware.use
only takes one callback argument.
formMiddleware.success(callback)
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.
formMiddleware.flash(category='danger')
Flash an error message if any of the fields are invalid.
Form objects are set on the req oject by the form middleware.
form
Form.data
The modified field data that has been sanitied by field middleware
Form.rawData
The unmodified field data
Form.errors
An object that contains {fieldname: [errorMessage, ... ]}
entries
Form.isValid()
Returns true if form.errors is not empty
Form.addError(key, message)
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
form = require('connect-forms-validator');
field = form.field;
s
form.field("username").trim().use(function(req, res, next, keyname, value){
field.use(callback)
callback
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.
field.bool()
Ensure the field is a boolean value
field.trim()
Call string trim method on the value
field.required([message])
Flag an error if the field is not given
field.flash()
Flash error if the field has an error
field.is(re [, message])
Match the field value against a regular expression
field.default(value)
Set the default value for this field
choices = function(choices [, message])
Restrict this field to an array of choices