0.01
No commit activity in last 3 years
No release in over 3 years
Uses the model validation logic you have already defined to generate javascript validation using jquery
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 1.2.9
 Project Readme

jquery-validator¶ ↑

Rails plugin to generate jquery validations based on model validations. Use this plugin to keep your validations DRY. This plugin requires Rails 3, Jquery, and the Jquery validation plugin.

Supported validations include:

* Presence
* Format
* Numericality
* Includes
* Excludes
* Length
* Confirmation
* Acceptance

Usage¶ ↑

% rails generate jquery_validator:install
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"
= javascript_include_tag "http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js"
= javascript_include_tag "jquery_validator"

class User < ActiveRecord::Base
  validate :login, :presence => true, :length => {:minimum => 3, :maximum => 10}
  validate :email, :presence => true
  validate :password, :presence => true, :confirmation => true, :length => {:minimum => 8}
  validates_acceptance_of :eula
end

<%= form_for(@user) do |f| %>
  <%=f.jquery_validators %>
  Login: <%= f.text_field :login %><br/>
  Password: <%= f.password_field :password %><br/>
  Confirm: <%= f.password_field :password_confirmation %><br/>
  Accept Terms <%= f.checkbox :eula %>
  <%= f.submit %>
<% end %>

Options¶ ↑

You can pass a :only argument to jquery_validations to only validate certain fields. All other options are converted to JSON and passed directly to the jquery validate() method. You can pass javascript functions as strings. For example

<%= f.jquery_validators :only => [:login, :email], :debug => true, :submitHandler => "function() { alert('Thank you!'); }" %>

If you want to be more unobtrusive, add

yield :javascripts

to the head section of your application.html.erb and surround your jquery_validators call with content_for like so:

<% content_for :javascripts do %>
  <%= f.jquery_validators %>
<% end %>

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 mixtli. See LICENSE for details.