Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rails plugin that maps model validations to class names on form elements to integrate with Validatious

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 generators
Octocat-spinner-32 lib
Octocat-spinner-32 rails
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rake_tasks~
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 ORIGINAL_README
Octocat-spinner-32 README.textile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
Octocat-spinner-32 VERSION
Octocat-spinner-32 validatious-on-rails.gemspec
README.textile

VALIDATIOUS-ON-RAILS

Rails plugin that maps model validations to class names on form elements to integrate with Validatious.

What Is Validatious?

Elevator pitch: A (kick-ass) unobtrusive client-side validation javascript library – the only one you’ll ever need.

http://validatious.org

…So What Is Validatious-On-Rails?

Elevator pitch: A (kick-ass) seamless “glue” between ActiveRecord and Validatious: Automatically maps models validations to client-side Validatious-validators on form elements in a obtrusive way using DOM classes. Makes your form-filling users happier, and you code cleaner and more scalable (tjing-tjing!). Why define client-side validations “once-again”, when they already defined in the model?

Features & Goals

Current Features:

  • Rails core validations
    • validates_acceptance_of – except: :on, :if/:unless
    • validates_confirmation_of – except: :on, :if/:unless
    • validates_exclusion_of – except: :on, :if/:unless
    • validates_format_of – except: :on, :if/:unless
    • validates_inclusion_of – except: :on, :if/:unless
    • validates_length_of – except: :on, :if/:unless
    • validates_numericality_of – except: :on, :if/:unless
    • validates_presence_of – except :on, :if/:unless
    • validates_size_of – except :on, :if/:unless
    • (More to come…)
  • Supports all Rails core form field helpers
  • Seamless Rails I18n integration (validation error messages)
  • Generator for getting started – easy as 1-2-3 (see below)
  • Ruby representation of v2.Validator – for custom validators (public API announced soon)
  • Client-side validations can easily be disabled for specific fields.
  • Ruby 1.9.x support (note: Rails 2.3.4 + Ruby 1.9.1 + validatious-on-rails don’t work – because of a known Rails 2.3.4-bug)
  • For the near fanatic Formtastic form-builder users like me: With default config, it’s compatible with the Formtastic semantic HTML structure.
  • Test-suit

Note: Just started so…expect the list to expand – especially validations. See the TODO for more info.

Goals:

  • Should support all Rails core validations (using preferably client-side validation, or last-resort solution using server-side validation – backed by Validatious)
  • Should be very easy to extend with custom validations (plugins, e.g. Validatious::Validators.add MyCustomValidator), but within the domain of Validatious (focus) – no vision of support multiple libraries (only makes the code base unnecessary big and complex)
  • Support all Rails core form helpers, but should also be easy to extend with custom form field helpers.
  • Should not only perform the validations, but also respect the Rails validation options
  • Should integrate with the Rails I18n API seamlessly
  • Should have solid test suit

Note: Somethings fundamental is missing? Let me know! =)

Installation

Gem:

sudo gem install validatious-on-rails --source http://gemcutter.org

and in config/environment.rb:

config.gem 'validatious-on-rails', :source => 'http://gemcutter.org'

Plugin:

./script/plugin install git://github.com/grimen/validatious-on-rails.git

Dependencies:

Note: For testing, get: context, rr, acts_as_fu, and redgreen.

Configuration

1. Generate required javascript files

$ ./script/generate validatious                                                                                                                                                              
      create  public/javascripts/v2.standalone.full.min.js
      create  public/javascripts/validatious.config.js
      create  config/initializers/validatious-on-rails.rb

2. Include the javascripts in your layout

app/views/application.html.erb or similar:

<head>
  ...
  <%= javascript_include_tag 'v2.standalone.full.min.js', 'validatious.config.js' # 1) %>
  <%= javascript_for_validatious # 2) %>
  ...
</head>

Notes:

  1. validatious.config.js is optional, but contains some default Valdiatious-configuration that easily can be tweaked or extended – see Validatious documentation.
  2. FYI, this is where custom validations gets inserted. No worries; the plugin is doing the magic!

Usage

Easy!

Consider you got some model validations:

class Post < ActiveRecord::Base
  validates_presence_of :title
  validates_length_of :title, :within => 3..20
  validates_length_of :body, :within => 3..20, :client_side => false
end

…then you now got working client-side validations for any forms with class validate (or any custom class – see documentation/config):

<% form_for(@post, :html => {:class => 'validate'}) do |f| %>
  <%= f.text_field :title %>
<% end %>

Validatious ♥ Formtastic

For all you Formtastic users out there: This plugin is out-of-the-box compatible with Formtastic – unlike the similar solution(s) out there. But to get Validatious and Formtastic to play well with each other, you need to tweak the configuration settings a bit. These settings are very much recommended:

Formtastic:

Formtastic::SemanticFormBuilder.inline_errors = :list
Formtastic::SemanticFormBuilder.inline_order  = [:input, :hints, :errors]

Validatious:

v2.Field.prototype.positionErrorsAbove = false;
v2.Fieldset.prototype.positionErrorsAbove = false;
v2.Fieldset.prototype.messagesClass = 'errors';
v2.Fieldset.prototype.failureClass = 'error';
v2.Fieldset.prototype.successClass = '';
v2.Field.prototype.messagesClass = 'errors';
v2.Field.prototype.failureClass = 'error';
v2.Field.prototype.successClass = '';

Limitations

This is very much work-in-progress, so there are some known limitations that should work very soon. For example some validations are not supported yet – either because it takes some tricks, or it’s not scalable without AJAX. See TODO.

Origin

This project was originally forked from:

http://github.com/cjohansen/validatious-on-rails

License

Original work:

Released under the MIT license.
Copyright © 2008 Christian Johansen

Additional work:

Released under the MIT license.
Copyright © 2009 Jonas Grimfelt

Something went wrong with that request. Please try again.