UKHomeOfficeForms / hof-form-wizard

MIT License
0 stars 2 forks source link

hof-form-wizard

Creates routing and request handling for a multi-step form process.

Given a set of form steps and field definitions, the wizard function will create an express router with routing bound to each step of the form and input validation applied as configured.

Additional checks are also applied to ensure a user completes the form in the correct order.

Usage

Define a set of steps:

// steps.js
module.exports = {
  '/step1': {
    next: '/step2'
  },
  '/step2': {
    next: '/step3',
    fields: ['name']
  },
  '/step3': {
    next: '/step4',
    fields: ['age']
  },
  '/step4': {}
}

Define field rules:

// fields.js
module.exports = {
  'name': {
    validate: 'required'
  },
  'age': {
    validate: 'required'
  }
}

Create a wizard and bind it as middleware to an app:

var wizard = require('hof-form-wizard'),
    steps = require('./steps'),
    fields = require('./fields');

app.use(wizard(steps, fields));

Sessions

The wizard expects some kind of session to have been created in previous middleware layers.

For production use a database backed session store is recommended - such as connect-redis.

Additional step options

The minimum amount of configuration for a wizard step is the next property to determine where the user should be taken after completing a step. A number of additional properties can be defined.

Additional field options

Remaining field options documentation can be found in the hof-template-mixins README.

Additional wizard options

A number of options can be passed to the wizard as a third argument to customise aspects of the behaviour for all steps.

translate - provide a function for translating validation error codes into usable messages. Previous implementations have used i18next to do translations. templatePath - provides the location within app.get('views') that templates are stored. Default pages.

Behaviours

Creating a behaviour:

// behaviour-one.js

module.exports = SuperClass => class extends SuperClass {
  getValues(req, res, callback) {
    super.getValues(req, res, (err, values) => {
      if (err) {
        return callback(err);
      }
      const errorValues = req.sessionModel.get('errorValues') || {};
      return callback(null, Object.assign({}, values, errorValues))
    });
  }
}
//index.js
const app = require('express')();
const Wizard = require('hof-form-wizard')
const BehaviourOne = require('./behaviours/behaviour-one')
const BehaviourTwo = require('./behaviours/behaviour-two')

app.use(Wizard({
  '/': {
    behaviours: [BehaviourOne, BehaviourTwo]
  }
}, {}, {}));

Predefined behaviours

Some behaviours are built-in to the Wizard, and can be declared by passing a string as a behaviour.

These are:

//index.js
const app = require('express')();
const Wizard = require('hof-form-wizard')

app.use(Wizard({
  ...
  '/confirm-submission': {
    behaviours: ['complete'],
    next: '/finished'
  },
  '/finished': {},
  '/download-receipt': {
    allowPostComplete: true
  }
}, {}, {}));