Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Validation groups for ActiveRecord
Ruby
branch: master

This branch is 3 commits ahead, 30 commits behind adzap:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
MIT-LICENSE
README.rdoc
Rakefile
grouped_validations.gemspec
init.rb

README.rdoc

Grouped Validations

Allows you to define validation groups in ActiveRecord for more control over what validations you want to run.

This can be useful for multi-page forms or wizard style data entry.

Works with Rails 2.3 and Rails 3 (ActiveRecord only).

Installation

Just install the gem

gem install grouped_validations

Add it to your Rails environment gems

config.gem 'grouped_validations'

Usage

Define validations as you would normally but inside a validation_group block which you pass a group name to.

class Person < ActiveRecord::Base
  validation_group :name do
    validates_presence_of :first_name
    validates_presence_of :last_name
  end

  validates_presence_of :sex
end

You can define validations outside the group as normal.

To check for errors for only a certain group of validations

p = Person.new
p.group_valid?(:name) # => false
p.first_name = 'John'
p.last_name = 'Smith'
p.group_valid?(:name) # => true

If you run the normal valid? method all validations, inside and outside validation groups, will be run.

p.valid? # => false because sex is not present

You can also check validation for multiple groups

p.groups_valid?(:group1, :group2)

To define validation blocks just use the respective group validation method, like so

class Person < ActiveRecord::Base
  validation_group :name do
    validates_presence_of :first_name
    validates_presence_of :last_name
  end

  validate_name           {|r| # something custom on save }
  validate_name_on_create {|r| # something custom on create }
  validate_name_on_update {|r| # something custom on update }
end

Credits

Copyright © 2010 Adam Meehan, released under the MIT license

Something went wrong with that request. Please try again.