aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations.rb
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-02-01 10:08:20 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-02-01 10:08:20 +1100
commita07d0f87863e01ef931c87bd35bd36c564c20cd3 (patch)
tree308f9e66ad1164cb4a8d3cd9ab9d6263a4828b0d /activemodel/lib/active_model/validations.rb
parentc493370f332715dee0ef795a66e896d7f0471cbe (diff)
downloadrails-a07d0f87863e01ef931c87bd35bd36c564c20cd3.tar.gz
rails-a07d0f87863e01ef931c87bd35bd36c564c20cd3.tar.bz2
rails-a07d0f87863e01ef931c87bd35bd36c564c20cd3.zip
Full update on ActiveModel documentation
Diffstat (limited to 'activemodel/lib/active_model/validations.rb')
-rw-r--r--activemodel/lib/active_model/validations.rb39
1 files changed, 38 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 276472ea46..03733a9c89 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -3,6 +3,41 @@ require 'active_support/core_ext/hash/keys'
require 'active_model/errors'
module ActiveModel
+
+ # Provides a full validation framework to your objects.
+ #
+ # A minimal implementation could be:
+ #
+ # class Person
+ # include ActiveModel::Validations
+ #
+ # attr_accessor :first_name, :last_name
+ #
+ # validates_each :first_name, :last_name do |record, attr, value|
+ # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
+ # end
+ # end
+ #
+ # Which provides you with the full standard validation stack that you
+ # know from ActiveRecord.
+ #
+ # person = Person.new
+ # person.valid?
+ # #=> true
+ # person.invalid?
+ # #=> false
+ # person.first_name = 'zoolander'
+ # person.valid?
+ # #=> false
+ # person.invalid?
+ # #=> true
+ # person.errors
+ # #=> #<OrderedHash {:first_name=>["starts with z."]}>
+ #
+ # Note that ActiveModel::Validations automatically adds an +errors+ method
+ # to your instances initialized with a new ActiveModel::Errors object, so
+ # there is no need for you to add this manually.
+ #
module Validations
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
@@ -18,8 +53,10 @@ module ActiveModel
# class Person
# include ActiveModel::Validations
#
+ # attr_accessor :first_name, :last_name
+ #
# validates_each :first_name, :last_name do |record, attr, value|
- # record.errors.add attr, 'starts with z.' if value[0] == ?z
+ # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
# end
# end
#