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

florm

v1.0.2

Published

form validation and serialisation

Downloads

6

Readme

form

Form validation and serialization.

##Installation

npm install florm

##Usage

####Init

Pass either a node reference or a selector pointing to a form to the constructor, along with an optional options dictionary.

var Florm = require('florm');
var form = Florm('#comment_form', options);

####Options

{
	'fieldErrorClasses': ['comment_form_error'],
	'formErrorClasses': ['comment_form_errors],
	'formErrorContainer': '#form_errors',
	'submit': true,
}
  • fieldErrorClasses should be an array of class names to add to errors applied to individual fields. Empty by default.

  • formErrorClasses should be an array of class names to add to form wide errors. Empty by default.

  • formErrorContainer should be a selector pointing to an element to append form wide errors. Empty by default.

  • submit should be a boolean. True by default.

Field errors are errors that correspond to individual input fields.

Form errors are form wide errors, or errors that depend on multiple fields. 'The passwords do not match' is an example of a form error.

Submit indicates whether the validation and submission process should be automated. If submit is true, form validation will occur automatically when the form element recieves a submit event. If validation passes, the serialized data will be passed to the function passed to form.success. If it fails, the error dictionary returned from form.validate and the serialized form will be passed to the function in form.fail if it has beend defined, and to form.errors if not.

####Serialization

var form_data = form.serialize();

form.serialize() returns a serialized representation of the form. Input values are mapped to properties that correspond to the 'name' attribute of the input.

Example form:

<form id="comment_form">
	<input name="text" value="This is a comment">
	<input name="username" value="fergus">
</form>

Would serialize to:

{ text: 'This is a comment', username: 'fergus' }

####Add validation

form.validate(function(attrs) {
	var errors = {};
	if (!attrs.text.trim()) {
		errors['text'] = 'Please enter a comment.';
	} else if (attrs.username.length > 60) {
		errors['username'] = 'Username length cannot exceed 60 characters.';
	}
	return errors;
});

The function passed to validate will recieve a serialized representation of the form as it's only parameter. It must return a dictionary with each property set to the corresponding input's name attribute. Use '__all__' to indicate a form error.

####Add a success callback

Add a function to call after form validation has successfully completed. It will recieve a fully serialized representation of the form.

form.success(function(data) {
	var url = this.element.action;
	ff.http.json.post(url, data, function(response) {
		addToUI(response);
	});
});

####Add a fail callback

Add a function to call after form validation has failed. It will recieve any errors returned from form.validate(), and a fully serialized representation of the form. If a fail callback is not provided, form.errors will be called instead, passing the errors.

form.fail(function(errors, serializedForm) {
	form.errors(errors)
});

Other methods

By default, Florm will automatically override the submit event of the form element it is attached to. If you'd like more control over the process, the following methods are also available. Remember to pass {'submit': false} in options.

####Submit the form

form.element.addEventListener('submit', function(event) {
	form.submit();
});

The submit method triggers validation. If validate returns a populated dictionary, the fail method will be called. Otherwise, the success method will be called.

####Apply errors

Florm.errors is a method that applies errors to form inputs. Given the following 'errors' dictionary:

{ text: 'Please enter a comment.' }

It will query the form for an element with the 'name' attribute set to text, and insert an error after it in the following form:

<span class="form-error {{options.fieldErrorClasses}}">{{errors[property]}}</span>

e.g.

<span class="form-error comment_form_error">Please enter a comment.</span>

Will be inserted into the dom after the following input:

<input name="text">

form.Errors will be called automatically as part of the validation process, unless a form.fail method has been set. This can be overcome by calling form.errors(errors) from within the form.fail function.

####Clear the form

form.clear();

The clear method removes any errors within the form, as well as resetting each input.

####Clear errors

form.clearErrors();

clearErrors removes errors from within the form, without resetting inputs.