0.9.707 • Published 7 years ago

form-spine v0.9.707

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Form Spine

Form Spine is a lightweight (~4kb, ~1.2kb gzipped) JavaScript form library with validation, error handling and fetch (Polyfilled with developit/unfetch)

It's promise-based, which makes running scripts on error/success very easy.


Table of Contents


Getting Started

Install Instructions

Adding Form Spine to your project requires NPM. Optinally you could use Yarn.

Run the following command in your project root:

npm install form-spine --save

Or with Yarn:

yarn add form-spine

Using In Your Project

Using Rollup or WebPack (or another module bundler), you can do like this:

// ES6
import FormSpine from "form-spine";

// CommonJS
var FormSpine = require("form-spine");

Remember to polyfill Fetch

require("unfetch/polyfill");

Usage

Vue.js example

let formFields = {
    todo_text: {
        required: true,
        max_length: 50
    }
};

new Vue({
    el: "#app",
    data: {
        form: new FormSpine('/create-todo', formFields)
    },
    
    methods: {
        submit: function() {
            this.form.post().then(() => alert('Done!'));
        }
    }
});

API

The FormSpine Class

The FormSpine class is the backbone of Form Spine and the class you'll be using.

Methods

MethodDescriptionParameters
postSends a POST request to the url specified in the Form object
deleteSends a DELETE/DESTROY request to the url specified in the Form object
putSends a PUT request to the url specified in the Form object
submitSends a request with the type specified, to the url specified in the Form objecttype: Any request type possible in the fetch api. Example: form.submit('GET')

Parameters

NameTypeDescriptionRequiredDefault
urlstringThe url that requests should be send to.true''
fieldsobjectThe fields in the form.true{}
optionsobjectAn object with additional optionsfalse{}
FormSpine options parameters
NameTypeDescriptionRequiredDefault
messagesobjectCustom error validation messages.false{}
resetOnSuccessbooleanDetermines if form fields should be cleared on success.falsefalse
headersobjectAdds custom headers to each requestfalse{}
url REQUIRED

The url parameter is the first of three parameters, and it defines which url to send requests to upon submit(). It can be an absolute or relative url, such as: /submit or https://your-site.com/send.

fields REQUIRED

The fields that you have in the form should be defined here as an object of objects, keyed by field name. This is also where you define validation rules (if any)

Example:

let fields = {
    username: {
        required: true
    },
    
    password: {
        required: true,
        min_length: 6,
        must_match: "password_confirmation"
    },
    
    password_confirmation: {
        required: true
    }
};

// Init form
let formObject = new FormSpine('/submit', fields);

Possible field attributes are:

NameTypeDescriptionDefault
valuestringThe initial value of the field''
namestringThe field name used in error messages (:field)object key name
requiredbooleanValidation rule: determines if field is requiredfalse
min_lengthintegerValidation rule: sets a minimum length for the field
max_lengthintegerValidation rule: sets a maximum length for the field
no_digitsbooleanValidation rule: may not contain digitsfalse
only_digitsbooleanValidation rule: may only contain digitsfalse
checkedbooleanValidation rule: has to be checked / be true (for checkbox/radio inputs)false
must_matchstringValidation rule: has to match another field (good for password confirmations)
regexstring/regexValidation rule: must match regex
customMessages OPTIONAL

This parameter is useful if you want to translate or change the validation messages. You have to pass in an object of strings with error messages.

Example (Using defaults):

let customMessages = {
    checked:      "The :field must be checked.",
    regex:        "The :field field is invalid.",
    required:     "The :field field is required.",
    no_digits:    "The :field field may not contain digits.",
    only_digits:  "The :field field may only contain digits.",
    must_match:   "The :field field match the :must_match field.",
    min_length:   "The :field field must be at least :min_length characters.",
    max_length:   "The :field field must not be longer than :max_length characters."
};

// Init form
let formObject = new FormSpine('/submit', {}, { messages: customMessages });

Examples

Vue Demo Preact Demo


Slim Spine

Don't care about client-side validation, and does every byte count? How about using Slim Spine?

It's a ~0.9kb version of Form Spine, without the Validation library. It still handles errors, but will not validate fields.

Get Slim Spine here <-- COMING SOON


Inspiration

I found myself creating similar classes for every new project I started, so I felt it was time to combine everything into a single class that I could use for almost all my projects. Of cause, in the nature of sharing, I decided to open source it.

Form Spine is inspired heavily on laracasts/vue-forms

If you're into learning, you should really go signup at Laracasts

Build scripts (and more) are heavily based on developit/unfetch.

0.9.707

7 years ago

0.9.706

7 years ago

0.9.703

7 years ago

0.9.702

7 years ago

0.9.62

7 years ago

0.9.6185

7 years ago

0.9.617

7 years ago

0.9.615

7 years ago

0.9.61

7 years ago

0.9.48

7 years ago

0.9.46

7 years ago

0.9.45

7 years ago

0.9.41

7 years ago

0.9.35

7 years ago

0.9.3

7 years ago

0.9.1

7 years ago

0.8.9

7 years ago

0.8.6

7 years ago

0.8.5

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.0

7 years ago

0.7.6

7 years ago

0.7.5

7 years ago

0.7.0

7 years ago

0.6.5

7 years ago

0.6.23

7 years ago

0.6.22

7 years ago

0.6.21

7 years ago

0.6.2

7 years ago

0.6.16

7 years ago

0.6.0

7 years ago

0.5.609

7 years ago

0.5.608

7 years ago

0.5.607

7 years ago

0.5.606

7 years ago

0.5.605

7 years ago

0.5.604

7 years ago

0.5.603

7 years ago

0.5.602

7 years ago

0.5.601

7 years ago

0.5.6

7 years ago

0.5.59

7 years ago

0.5.58

7 years ago

0.5.57

7 years ago

0.5.56

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago