0.5.1 • Published 7 years ago

phalange v0.5.1

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

Phalange

Phalange is a lightweight (700 bytes gzipped) JavaScript form library with 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 Phalange to your project requires NPM. Optinally you could use Yarn.

Run the following command in your project root:

npm install phalange --save

Or with Yarn:

yarn add phalange

Using In Your Project

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

// ES6
import Phalange from "phalange";

// CommonJS
var Phalange = require("phalange");

Remember to polyfill Fetch

require("unfetch/polyfill");

It's also on unpkg:

<script src="//unpkg.com/phalange/dist/phalange.umd.js"></script>

<script>
var Phalange = phalange; // to fix name in UMD package, for consistency.

new Phalange('/', {});
</script>

Please notice that the fetch polyfill is NOT included in the UMD version.


Usage

Vue.js example

let fields = {
    todo_text: ""
};

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

API

The Phalange Class

The Phalange class is the core of Phalange 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{}
Phalange 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 submitting. 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 an object of keys with a value (the default input value)

Example:

let fields = {
    username: "",
    password: ""
};

// Init form
let formObject = new Phalange('/login', fields);

Examples

Vue Demo TODO

Preact Demo TODO

Angular Demo TODO


FormSpine

Want client-side validation? Try FormSpine?

It's a larger version of Phalange, with a Validation library. It's almost the same API, but with more functionality.

Get FormSpine 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.

Phalange 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.