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