Created
April 6, 2012 21:37
-
-
Save leommoore/2323136 to your computer and use it in GitHub Desktop.
Rails Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validates_presence_of | |
# Attribute must not be blank (nil, false, "", " ",[] ,{}) | |
# :message => "can't be blank" | |
validates_length_of | |
# Attribute must meet the length requirements of the options | |
# :is, :minimum(integer) | |
# :within, :in(range) | |
# :wrong_length => "is the wrong length (should be {{count}} characters)" | |
# :too_short => "is too short (minimum is {{count}} characters)" | |
# :too_long => "is too long (maximum is {{count}} characters)" | |
validates_numericality_of | |
# Attribute must be an integer or a floating point number | |
# :equal_to, :greater_than, :less_than, :greater_than_or_equal_to, :less_than_or_equal_to(numeric) | |
# :odd, :even, :only_integer(boolean) | |
# :message => "is not a number" | |
validates_inclusion_of | |
# Attribute must be in a list of choices (an array or range) | |
# :in(enumerable) | |
# :message => "is not included in the list" | |
validates_exclusion_of | |
# Attribute must not be in the list of choices (an array or range) | |
# :in(enumerable) | |
# :message => "is reserved" | |
validates_format_of | |
# Attribute must match a regular expression | |
# :with(regular expression) | |
# :message => "is invalid" | |
validates_uniqueness_of | |
# Attribute must not already exist in the database(for the given scope) | |
# :case_sensitive(boolean) | |
# :scope(column symbols for limiting scope) | |
# :message => "has already been taken" | |
validates_acceptance_of | |
# Attribute must be "accepted" | |
# Creates a virtual attribute if there is no table column for the attribute | |
# :accept(expected value, "1") | |
# :message => "must be accepted" | |
validates_confirmation_of | |
# Attribute must be confirmed by entering it twice | |
# Creates a virtual attribute for the confirmation (:email gets :email_confirmation) | |
# Only validates when confirmation attribute is not nil (no confirm form field submitted = no validation) | |
# :message => "doesn't match confirmation" | |
#Options | |
:allow_nil => true | |
# Skips validation if the attribute is nil (no value set) | |
:allow_blank => true | |
# Skips validation if attribute is blank (nil, false, "", " ", [], {}) | |
:on => :save / :create / :update | |
# Only validates if this is a new record(:create) or an existing record (:update) | |
# :save validates in both cases and is the default | |
:if => :method / :unless => :method | |
# :method is a method defined in the model that should return true or false | |
#Rails 3 Method for combining validations | |
validates :email, :presence => true, | |
:length => { :maximum => 50 }, | |
:uniqueness => true, | |
:format => { :with => EMAIL_REGEX }, | |
:confirmation => true | |
validates_associated(association, options) | |
# Associated object or objects must all be valid | |
# :message => "is invalid" | |
# :on => save / :create / :update, :if => method, :unless => method | |
#cannot be used with Rails 3 combined validations | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment