The API provided for ActiveRecord validations gives developers a way to describe valid model object state. The validations hook into the object life cycle and inspect object state to determine the validity of attributes. If any validations fail, they will be reported as errors in the model, which are accessible through the errors attribute, an instance of ActiveRecord::Errors. If there are no errors on an ActiveRecord model, the errors collection is empty, and the valid? method would return true. The validations you can use will appear like declarations; they are simply class methods. Each validation method accepts n attributes, along with an optional set of options. Any bang! methods that are called on the model that fail will raise an error with the description of the failures.
validates_acceptance_of :some_attribute # attribute must be accepted.
validates_acceptance_of :some_attribute, :accept=>’YES’ # considered acceptance. default=1
validates_associated :has_many_association # the valid? method will be called on each object
# creates a virtual attribute_confirmation – gets compared to attribute.
validates_confirmation_of :attribute # the user interface must use attribute_confirmation
validates_each :attribute_one, :attribute_two, :attribute_etc do |record, attr, value|
# you can give validates_each an array of attributes and a block to perform.
# The block marks the object as valid or not by adding to the errors array or not.
# example…. record.errors.add(attr) unless PurchasingSystem.validate(attr,value)
end
validates_inclusion_of :gender, :in=>['m','f'], :message => ‘Enter m or f.’
validates_exclusion_of :login, :in=>['admin', 'root', 'superuser'], :message=>’No no no.’
# Available as a plugin… optional :allow_nil => false
validates_existence_of :attribute # checks foreign key in belongs_to refrences existing record
# script/plugin install http://svn.hasmanythrough.com/public/plugins/validates_existence
validates_format_of :email, :with=> … #some regular expression
# provides optional :wrong_length, :too_long, and :too_short options
validates_length_of :login, :minimum => 5 # minimum value
validates_length_of :login, :within => 5..20 # a Ruby Range
validates_length_of :account_number, :is => 16 # exact value
validates_length_of :account_number, :is => 16, :wrong_length => “should be %d characters.”
validates_numericality_of :some_numeric_attribute, :integer_only => true
validates_presence_of :some_attr_checked_if_blank # won’t allow nil or “”
validates_uniqueness_of :attribute_is_unique_for_all_models_of_same_type_via_query
validates_uniqueness_of :line_two, :scope => [:line_one, :city, :zip]
validates_uniqueness_of :login_name, :case_sensitive => false
COMMON VALIDATION OPTIONS
:allow_nil => true # skips validation if the value of the attribute is nil
:if => … # conditional validation
:message => … #specific message describing the validation failure, overriding the default msg
: on => … # by default, validations are run on save. specify create for validates_uniqueness_of
THE ERRORS COLLECTION
Errors for attribute validations will be created by prepending the canned validation messages with the name of the associated model.
add_to_base(msg) # adds complete msg to the object state for self; not any particular attribute
add(attribute,msg) # adds partial msg to a particular attribute
clear # clears the errors collection
invalid?(attribute) # true or false depending on if validation errors are associated
on(attribute) # nil,string or array depending on the errors collection state for the attribute
CUSTOM VALIDATION – Rails provides three hook methods for custom validation
validate # called after standard Rails validations
validate_on_create # called if new_record?
validate_on_update # called if not a new_record?
SKIPPING VALIDATION
The ActiveRecord::Validations module mixed into ActiveRecord::Base affects three instance methods; save, save! and update_attribute are affected. Validations can be skipped by passing in false as a method parameter. As a result of ActiveRecord::Validations calling :alias_method_chain :save, :validation … you end up with save_without_validations. Be careful — update_attributes does use validations, while the singular form, update_attribute, does not.
