Giter VIP home page Giter VIP logo

jquery.form.serializer's Introduction

jQuery Form Validation

This jQuery extension provides an easy way to serialize HTML forms into JSON.

By default the serialization it's based on the submittable fields according to W3C Successful controls, but this is easily customizable to fit your needs.

The following elements are always ignored:

  • Elements without a name attribute.
  • File inputs.
  • Buttons.

Installation

Include jquery.form.serializer.min.js after jquery.js.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.form.serializer.min.js"></script>

Usage

Based on a form like this one:

<form id="my-form">
  <input type="hidden" name="token" value="ABC" />
  <input type="text" name="user[name]" value="John Doe" />
  <input type="text" name="user[email]" value="[email protected]" />
  <select name="user[country]">
    <option value="CL" selected>Chile</option>
  </select>
</form>

Serialize the form into JSON:

$("#my-form").getSerializedForm();

// =>
{
  token: "ABC",
  user: {
    name: "John Doe",
    email: "[email protected]",
    country: "CL"
  }
}

Submittable Fields

The submittable fields are initially selected using:

$.jQueryFormSerializer.submittable = 'input, select, textarea';

The initial matched set it's reduced passing every function defined in $.jQueryFormSerializer.filters to the filter function.

There are two default filters:

  • enabledOnly: Disabled fields won't be serialized.
  • checkedOnly: Only checked input[type="checkbox"] and input[type="radio"] will be serialized.

Value Castings

Value castings are defined in $.jQueryFormSerializer.castings, and allows you to preprocess a field value before serializing it.

The only default value casting it's booleanCheckbox, that returns true or false on checkboxes without an explicit value attribute.

Customization

Any option declared in $.jQueryFormSerializer can be overwritten if you need a global customization, or you can pass a hash of options to getSerializedForm that will extend $.jQueryFormSerializer, allowing to change the defaults only for one call.

Examples

Always allow disabled fields to be serialized:

$.jQueryFormSerializer.filters.enabledOnly = false;

Allow unchecked fields to be serialized only for this call:

$("#my-form").getSerializedForm({
  filters: {
    checkedOnly: false
  }
});

Add a new filter to avoid serializing fields with .disabled:

$.jQueryFormSerializer.filters.disabledByClass = function() {
  return !$(this).hasClass("disabled");
};

Add a new value casting for numeric fields:

$("#my-form").getSerializedForm({
  castings: {
    numericField: function() {
      if ($(this).hasClass("numeric")) {
        return parseInt($(this).val());
      }
    }
  }
});

The same applies to filters, castings and the submittable selector.

Custom Controls

You can easily integrate any custom control for serialization. For example, given this custom control:

<form id="my-form">
  <div class="custom-control" name="my-custom-control" data-custom-value="my value"></div>
</form>
$.valHooks.custom_control = {
  get: function(el) {
    return $(el).data("custom-value");
  },
  set: function(el, value) {
    return $(el).data({ "custom-value": value });
  }
};

$.fn.customControl = function() {
  return $(this).each(function() {
    this.type = "custom_control";

    // All your custom control magic...
  });
};

$(function() {
  $(".custom-control").customControl();
});

Add your custom control to the global configuration:

$.jQueryFormSerializer.submittable += ", .custom-control"

And that's it!

$("#my-form").getSerializedForm(); // => { "my-custom-control": "my value" }

$(".custom-control").addClass("disabled");
$("#my-form").getSerializedForm(); // => {}

Tests

Run npm install and npm test, or if you don't have Node.js installed, open ./specs/index.html on any browser.

jquery.form.serializer's People

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.