Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bleskomat/form

bleskomat2.7kMIT1.3.2

Form class for node.js with validation (synchronous and asynchronous), post-processing, and support for handlebars templates.

readme

form-node

Build Status

Form class for node.js with validation (synchronous and asynchronous), post-processing, and support for handlebars templates.

Installation

Add to your application via npm:

npm install @bleskomat/form

Usage

To use with express-handlebars:

const bodyParser = require('body-parser');
const express = require('express');
const Form = require('@bleskomat/form');
const Handlebars = require('express-handlebars');
const path = require('path');
const { ValidationError } = Form;

const app = express();

const hbs = Handlebars.create({
    extname: '.html',
    helpers: Form.handlebars.helpers,
    partialsDir: [
        path.join(__dirname, 'views', 'partials'),
        Form.handlebars.partialsDir,
    ],
});

app.engine('.html', hbs.engine);
app.set('view engine', '.html');
app.set('views', path.join(__dirname, 'views'));
app.enable('view cache');

// Parse application/x-www-form-urlencoded:
app.use(bodyParser.urlencoded({ extended: false }));

const form = new Form({
    title: 'Form partial example',
    action: '/form',
    groups: [
        {
            name: 'login',
            inputs: [
                {
                    name: 'username',
                    label: 'Username',
                    required: true,
                    validate: function(value, data) {
                        // `value` contains the value submitted for this field.
                        // `data` is an object which contains all form data.
                        // Perform custom validations for this field here.
                        // Throw an error here to fail the validation.
                        // Optionally return an instance of Promise to perform asynchronous validation.
                    },
                    process: function(value) {
                        // `value` contains the value submitted for this field.
                        // Perform custom processing for this field's value.
                        return value;
                    },
                },
                {
                    name: 'password',
                    label: 'Password',
                    required: true,
                },
            ],
        },
    ],
});

app.get('/form', function(req, res, next) {
    res.render('form', {
        form: form.serialize(),
    });
});

app.post('/form', function(req, res, next) {
    // `req.body` is provided by the `bodyParser` middleware.
    form.validate(req.body).then(values => {
        // Validation successful.
        // `values` is an object which contains processed form data.
    }).catch(error => {
        if (error instanceof ValidationError) {
            res.status(400);
        } else {
            error = new Error('An unexpected error has occurred');
            res.status(500);
        }
        res.render('form', {
            form: form.serialize({
                errors: [ error.message ],
            }),
        });
    });
});

Tests

Run automated tests as follows:

npm test

Changelog

See CHANGELOG.md

License

This software is MIT licensed:

A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source. There are many variations of this license in use.

changelog

Changelog

  • v1.3.2:
    • Downgraded express-handlebars due to incompatibility with nodejs <16
  • v1.3.1:
    • Updated dependencies
  • v1.3.0:
    • Added "autocomplete" input attribute
    • Updated dependencies, removed underscore
  • v1.2.5:
    • Updated dependencies
  • v1.2.3:
    • Added "file" input type
  • v1.2.2:
    • Normalize class names of form groups, rows
  • v1.2.1:
    • Fix for missing breaklines helper for textarea field type
  • v1.2.0
    • Removed registerPartials
    • Added new form options: "buttons", "instructionsHtml"
  • v1.1.0:
    • Added handlebars helpers (switch, case, default) that can be used with handlebars or express-handlebars
  • v1.0.0:
    • Initial release