npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hof-form-wizard

v5.1.1

Published

routing and request handling for a multi-step form processes

Downloads

49

Readme

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.

  • fields - specifies which of the fields from the field definition list are applied to this step. Form inputs which are not named on this list will not be processed. Default: []
  • template - Specifies the template to render for GET requests to this step. Defaults to the route (without trailing slash)
  • backLink - Specifies the location of the step previous to this one. If not specified then an algorithm is applied which checks the previously visited steps which have the current step set as next.
  • behaviours - A single behaviour, or an array of behaviours to be mixed into the base controller to extend functionality. If an array of behaviours is given, they are applied left-to-right, so calling super in the right-most behaviour will point to the previous behaviour in the array.
  • forks - Specifies a list of forks that can be taken depending on a particular field value or conditional function - See handling forking journeys in hof-form-controller.
  • allowPostComplete - If set to true allows a step to be accessed after the application has been completed. If not set then accessing this step will reset the session.

Additional field options

  • invalidates - an array of field names that will be 'invalidated' when this field value is set or changed. Any fields specified in the invalidates array will be removed from the sessionModel. Further to this any future steps from the invalidating step field will be removed from the sessionModel.

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.

  • controller - The constructor for the controller to be used for request handling. The default is an extension of the hof-form-controller, which is exported as a Controller property of this module. If custom behaviour is required for a particular form step then custom extensions can be defined by passing a list of Behaviours - see Behaviours params - define a suffix for the routes for supporting additional URL parameters. formatters - defines a standard list of formatters to be applied to all field data. Default: ['trim', 'singlespaces', 'hyphens'] - see formatters.js for a list of available formatters.

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:

  • complete - if applied to a step then this step will mark the application as complete. The user will then have their session reset if they attempt to access any step of the form. The next step of a completing step will always be accessible following completion. Any other steps which should also be accessible on a complete session should have their allowPostComplete option set to true.
//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
  }
}, {}, {}));