From 3cb0283fb6547982b35fda1e0d4af00071c026ae Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 19 Jan 2010 11:29:01 +1100 Subject: Documentation cleanup and linkage for validator --- activemodel/README | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activemodel/README') 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 -- cgit v1.2.3