Wizard plugin
Support validating multiple steps form
Usage
Assume that the form is split into multiple steps which can be accessed via a CSS class js-step. It also contains two buttons for jumping to the previous or next step.
<form id="demoForm" method="POST">
<div class="js-step">...</div>
<div class="js-step">...</div>
<div class="js-step">...</div>
<button id="prevButton">Prev</button>
<button id="nextButton">Next</button>
</form>
Clicking the Next button will validate the current step. If all fields in the step are valid, the plugin will bring user to the next step.
The following piece of code is the starting point to use the Wizard plugin:
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.3/css/uikit.min.css" />
<link rel="stylesheet" href="/vendors/formvalidation/dist/css/formValidation.min.css" />
</head>
<body>
<form id="demoForm" method="POST">...</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>
<script src="/vendors/formvalidation/dist/js/FormValidation.min.js"></script>
<script src="/vendors/formvalidation/dist/js/plugins/Wizard.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
FormValidation.formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
wizard: new FormValidation.plugins.Wizard({
stepSelector: '.js-step',
prevButton: '#prevButton',
nextButton: '#nextButton',
}),
...
},
}
);
});
</script>
</body>
</html>
Options
The Wizard plugin provides the following options:
(* denotes a required parameter)
| Option | Type | Description |
|---|
isFieldExcluded | Function | Indicate which fields should be execluded |
isStepSkipped | Function | Skip a given step |
nextButton * | String | A CSS selector of the Next button |
prevButton * | String | A CSS selector of the Previous button |
stepSelector * | String | A CSS selector of steps |
Exclude a field
The isFieldExcluded option is a function that takes three parameters and returns true or false:
FormValidation.plugins.Wizard({
isFieldExcluded: function(field: string, element: HTMLElement, elements: HTMLElement[])
},
})
Skip a step
The isStepSkipped option is a function that takes skipStepProps parameter and returns true or false:
FormValidation.plugins.Wizard({
isStepSkipped: function (skipStepProps) {
},
});
Events
The Wizard plugin provides four events that you can trigger. The event handlers can be set via either plugin option
FormValidation.formValidation(form, {
fields: {
...
},
plugins: {
wizard: new FormValidation.plugins.Wizard({
onStepActive: function(e) {
},
onStepInvalid: function(e) {
},
onStepValid: function(e) {
},
onValid: function(e) {
},
}),
},
});
or using on() as following:
FormValidation
.formValidation(form, {
fields: {
...
},
plugins: {
wizard: new FormValidation.plugins.Wizard({
stepSelector: '...',
prevButton: '...',
nextButton: '...',
}),
},
})
.on('plugins.wizard.step.active', function(e) {
})
.on('plugins.wizard.step.invalid', function(e) {
})
.on('plugins.wizard.step.valid', function(e) {
})
.on('plugins.wizard.valid', function(e) {
});
APIs
The plugin provides the following methods:
| Method | Description |
|---|
goToNextStep() | Jump to the next step. It's useful when users want to go to the next step automatically when a checkbox/radio button is chosen |
goToPrevStep() | Jump to the previous step |
const fv = FormValidation.formValidation(form, {
fields: {
...
},
plugins: {
wizard: new FormValidation.plugins.Wizard(),
},
});
const wizardPlugin = fv.getPlugin('wizard');
wizardPlugin.goToNextStep();
Submitting form
You cannot use the
DefaultSubmit plugin to submit the form when all steps are valid. Instead, you can use the
onValid option (or listen the
plugins.wizard.valid event):
FormValidation.formValidation(form, {
fields: {
...
},
plugins: {
wizard: new FormValidation.plugins.Wizard({
onValid: function(e) {
form.submit();
},
}),
},
});
Basic example
Changelog
- Allow to define the excluded field with the new
isFieldExcluded option, so we can animate the current step when jumping between steps - Provide the ability of skipping a step via new
isStepSkipped option
- Added new APIs to go to the previous or next steps
- Fixed a bug that the Wizard plugin doesn't work on IE 11
- First release. It means that the Wizard plugin requires FormValidation v1.3.0 or newer