Code Monkey home page Code Monkey logo

forms-js Build Status Join the chat at https://gitter.im/forms-js/forms-js

Forms JS is a form validation library intended to provide a backbone for other, framework-specific libraries.

Overview

To use Forms JS for validation, begin by creating an instance of Form and providing it with validation rules and a form-data object.

var formsjsForm = new formsjs.Form();
formsjsForm.formData = { // Input fields should read/write data to this object.
  username: "bvaughn"    // Default values can be provided.
};
formsjsForm.validationService = { // Maps field-names to validation rules.
  username: {                     // It should mirror the structure of form-data.
    required: true,
    type: "string"
  }
};
formsjsForm.submitFunction = function() {
  // You provide this function.
  // It is responsible for submitting your form data and returning a Promise.
  // This Promise should resolve/reject based on the response you receive after submitting.
};

Next you should wire your form inputs (the view) up with the Form instance so it can validate your data. (Note that Forms JS doesn't provide any view elements, only validation.) Each web component/Angular directive/React component/whatever should register itself like so:

var attributeMetadata = formsjsForm.registerAttribute('username');

// AttributeMetadata defines an errorMessages Array.
// You should bind to it in your view as it is used to show validation errors.
attributeMetadata.errorMessages;

// AttributeMetadata exposes a method named validate().
// You should call it any time your view updates the field's value.
// It will (asynchronously) update the 'errorMessages' property.
attributeMetadata.validate();

You'll also want to override a form-submit event so that Form can require a valid state. If the current form-data is valid it will be submitted using the submitFunction you provided earlier.

var formElement = document.getElementById('yourForm');

// Angular/jQuery style
$(formElement).on("submit", function() {
  formsjsForm.submitIfValid();

  return false;
});

// Plain JavaScript
formElement.addEventListener("submit", function() {
  event.preventDefault();

  formsjsForm.submitIfValid();
}), false);

Installation

Forms JS is available on both Bower and NPM under the name forms-js. You can install the library like so:

bower install forms-js
npm install forms-js

Building from Source

First install the toolchain with NPM:

npm install

Now you can build source like so:

npm run build

And run tests like so:

npm run test:watch

Note that tests are run on built source so be sure to build first.

forms-js's Projects

after.js icon after.js

Next.js-like framework for server-rendered React apps built with React Router 4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.