Giter VIP home page Giter VIP logo

ajaxform's Introduction

AjaxForm Plugin

This is jQuery plugin that helps submiting and validating your forms asynchronously. Keep your validation logic on server side, and for client side validate with javascript and AJAX.

Usage

Using ajaxForm plugin is fairly easy:

Just include plugin script into head section within script tag. And then call:

  $('#form').ajaxForm();

Optionaly ajaxFom function takes a hash of paratmeters:

  $('#form').ajaxForm({
    errorClass: 'error-field', // Error class applied to field if it failed validation
    showErrorMessage: true,    // Show error messages or just highlight field 
    errorMessageFormat: '<div class="error-message">{message}</div>', // Error message markup.
    insertMessage: 'before',   // Error message position. Accepts 2 options 'before' and 'after'
    onSuccess: function(){},     // Success callback. If validation passed
    onErrors: function(){},  //is callback for successfull server response with array of validation errors
    onError: function(){},  //is server error callback
  });

Or a single success callback function:

  $('#form').ajaxForm(function(){
    document.location.href = '/some_url';
  });

Optionaly you can specify redirect option in server response. So if JSON response contains redirect: '/some_url' it would be automaticaly triggered without any additional callbacks or configuration

Backend

In order to make ajaxForm correctly apply validation errors to form, server should respond with JSON containing errors in speified format.

Response example with errors:

  {errors: {email: "Invalid email format", username: "Username is already taken"}}

Multiple errors can be specified to the same field. But only the first one will be shown as an error message.

  {errors: {phone: ["Phone is too short", "Phone should be in numeric format"]}}

Ruby on Rails

If you are using Rails, generating error hash is as simple as:

  render :json => {:errors => @model.errors}

ajaxform'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.