FormUp
A lightwight form utlitiy module.
Validation
FormUp enhances HTML5's form validation, adding valid
and invalid
classes as the form is completed, and handling custom messages with a bit more finesse.
Automatic validation
FormUp can watch for form submission events and stop them and style invalid
inputs, and add messages if the form is invalid. This will also watch for
input changes and update validation styles when the user interacts.
Here's what that looks like.
var formUp = require( 'compose-form-up' )
Whenever submit
is triggered on a form, validation will
automatically run, handling messages and styles.
validate( form )
- Manual Validation
This component works best automatically, but if for some reason you have to
manually trigger validation, you'd do this.
var formUp = require( 'compose-form-up' )
var form = document.querySelector( '#your-form' )
if ( formUp.validate( form ) ) {
} else {
}
Here, formUp.validate( form )
will return true
or false
based on the forms
validity. It will set custom messages and style invalid inputs. However, to clear
invalid styles you'll need to run this any time a form element is updated.
Progressive Forms
A progressive form will show one fieldset at a time. Each time a submit event is triggered, the active fieldset will transition away (assuming there are no
validation errors), and the next fieldset will appear. Once all fieldsets are complete, the form will submit as usual. You can also register a callback to be
triggered at the point of each transition, to call some scrip before continuing (like an ajax call or whatever).
Here's what a simple progressive form may look like.
<form id="some-form" class="progressive">
<fieldset class="form-step">
<button type='submit'>Submit</button>
</fieldset>
<fieldset class="form-step">
<button type='submit'>Submit</button>
</fieldset>
</form>
Handling the next event
var formUp = require( 'compose-form-up' )
var form = document.querySelector( '#some-form' )
formUp.next( form, function( event, step ) {
ajax.post( '/users' )
.send( step.formData )
.end( function( err, response ) {
if (!err) step.forward()
else step.back()
})
})