aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-01-19 11:29:01 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-01-19 11:29:01 +1100
commit3cb0283fb6547982b35fda1e0d4af00071c026ae (patch)
treedd7413bfa82a9f2154706b4b94f970a5acfd8774
parent907754d7ee74c4f6f94c2457bd46e116bc82acbd (diff)
downloadrails-3cb0283fb6547982b35fda1e0d4af00071c026ae.tar.gz
rails-3cb0283fb6547982b35fda1e0d4af00071c026ae.tar.bz2
rails-3cb0283fb6547982b35fda1e0d4af00071c026ae.zip
Documentation cleanup and linkage for validator
-rw-r--r--activemodel/README23
-rw-r--r--activemodel/lib/active_model/validator.rb5
2 files changed, 26 insertions, 2 deletions
diff --git a/activemodel/README b/activemodel/README
index d2b51e2390..cf103b8d6d 100644
--- a/activemodel/README
+++ b/activemodel/README
@@ -193,3 +193,26 @@ functionality from the following modules:
{Learn more}[link:classes/ActiveModel/Validations.html]
+* Make custom validators
+
+ class Person
+ include ActiveModel::Validations
+ validates_with HasNameValidator
+ attr_accessor :name
+ end
+
+ class HasNameValidator < ActiveModel::Validator
+ def validate(record)
+ record.errors[:name] = "must exist" if record.name.blank?
+ end
+ end
+
+ p = ValidatorPerson.new
+ p.valid? #=> false
+ p.errors.full_messages #=> ["Name must exist"]
+ p.name = "Bob"
+ p.valid? #=> true
+
+ {Learn more}[link:classes/ActiveModel/Validator.html]
+
+ \ No newline at end of file
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 382a4cc98d..ad9729de00 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -1,5 +1,6 @@
module ActiveModel #:nodoc:
- # A simple base class that can be used along with ActiveModel::Validations::ClassMethods.validates_with
+ # A simple base class that can be used along with
+ # +ActiveModel::Validations::ClassMethods.validates_with+
#
# class Person
# include ActiveModel::Validations
@@ -28,7 +29,7 @@ module ActiveModel #:nodoc:
# end
#
# class MyValidator < ActiveModel::Validator
- # def validate
+ # def validate(record)
# record # => The person instance being validated
# options # => Any non-standard options passed to validates_with
# end