Skip to content

yuriineb/ajax_validation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AjaxValidation

AjaxValidation allows easy client side form validation for Rails 4.

Installation

Add this line to your application's Gemfile:

gem 'ajax_validation', github: 'gordienko/ajax_validation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ajax_validation

Include ajax_validation javascript assets

Add the following to your app/assets/javascripts/application.js:

//= require ajax_validation

Localization:

//= require ajax_validation/jquery.validate.localization/messages_xx

Theme:

//= require ajax_validation/theme_bootstrap3

Mount the engine in your routes file, as follows:

# config/routes.rb
mount AjaxValidation::Engine => "/ajax_validation"

Usage

Add a simple validation to your model.

# app/models/project.rb
class Project < ActiveRecord::Base
  validates :name, :presence => true,  uniqueness: { case_sensitive: false }, :length => { maximum: 10 }
end

Create app/assets/javascripts/validate.js.coffee

jQuery ->

  token = $('meta[name="csrf-token"]').attr('content')
  $.ajaxPrefilter((options, originalOptions, xhr)->
    xhr.setRequestHeader('X-CSRF-Token', token)
  )

  validate_url = '/ajax_validation/validate'
  id = null

  form = $('#new_project, [id^=edit_project_]')
  action = form.attr('action')
  id = action.split('/')[2] if typeof action != "undefined"
  form.validate(
    debug: false
    onkeyup: false
    rules:
      'project[name]':
        required: true
        remote:
          url: validate_url
          data:
            id: id
          type: 'post'
  )

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 96.4%
  • Ruby 3.6%