Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Some reusable ActiveModel validations, including greater_than, boolean_presence, and at_least_one_present
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config/locales
lib
spec
.gitignore
.rspec
Gemfile
Rakefile
Readme.md
activemodel-validators.gemspec

Readme.md

ActiveModel Validators

A collection of Rails 3 ActiveModel Validators

Example Usage

class Thing < ActiveRecord::Base
  validates :birthdate, greater_than: { value: Date.new(1900), message: 'must be later than 1900' }
  validates :birthdate, less_than:    { value: Time.zone.now.years_ago(1).to_date+1, message: 'must be at least 1 year ago' }
  validates :end_date,  greater_than: { attr: :begin_date, operator_text: 'later than' }

  # shortcut for        greater_than: { value: 1, operator: :>= }
  validates :how_many_pies, at_least: { value: 1 }

  # shortcut for less_or_greater_than: { attr: :how_many_people, operator: :== }
  validates :how_many_gifts, equal_to: { attr: :how_many_people }

  # Because presence: true doesn't work for boolean attributes!
  validates :is_18_or_older, boolean_presence: true

  validates :model,  restrict_to: {allowed_options: ['Model A', 'Model B']}, allow_blank: true

  validates_with AtLeastOnePresentValidator, at_least_one_of: [:home_phone, :work_phone]
  validates_with AtLeastOnePresentValidator, at_least_one_of: [:parent_1_home_phone, :parent_1_work_phone, :parent_1_mobile_phone], message: :at_least_one_phone_parent_1

  validates :option_b, blank: {message: :must_be_blank_if_option_a}, if: :option_a?

  validates :rating, multiple_of: {of: '0.5'}

  validates :total_profit, sum_of: { attr_names: (1..4).map {|quarter| :"total_profit_q#{quarter}" } }

  validates :end_date, date: {required: true}
  validates :graduation_year,       year: true

  validates :phone,                 phone: true
  validates :address_postal_code,   postal_code: true
  validates :address_state,         address_state: true
end

Documentation

Installation

Add to your Gemfile :

gem 'activemodel-validators'

Comparison to other validation gems/collections

I like this idea from https://github.com/fnando/validators:

    class Server < ActiveRecord::Base
      validates_datetime :starts_at
      validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
      validates_datetime :ends_at, :after => :now
      validates_datetime :ends_at, :before => :today

      validates :starts_at, :datetime => true
    end

Other collections worth taking a look at:

Other collections not worth taking a look at:

Specialized validators that stand on their own:

To do

  • Write actual documentation
  • Add tests
  • Merge into/with an other existing gem

License

Copyright 2012, Tyler Rick

This is free software, distributed under the terms of the MIT License.

Something went wrong with that request. Please try again.