aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides
diff options
context:
space:
mode:
authorJeff Dean <jeff@zilkey.com>2009-08-09 03:51:23 -0400
committerJeff Dean <jeff@zilkey.com>2009-08-09 03:51:23 -0400
commitebd6ef3b36c2428723fae5d630f9ad6800f037c0 (patch)
treeb03124f7872b3c875402e0154872f3818171d022 /railties/guides
parent47a3701df9b9fbe456c5ede83333320f9456e7f5 (diff)
downloadrails-ebd6ef3b36c2428723fae5d630f9ad6800f037c0.tar.gz
rails-ebd6ef3b36c2428723fae5d630f9ad6800f037c0.tar.bz2
rails-ebd6ef3b36c2428723fae5d630f9ad6800f037c0.zip
Added section to guide for validates with, which is scheduled for rails 3. See related ticket at:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2630-adding-a-validates_with-method
Diffstat (limited to 'railties/guides')
-rw-r--r--railties/guides/source/activerecord_validations_callbacks.textile41
1 files changed, 41 insertions, 0 deletions
diff --git a/railties/guides/source/activerecord_validations_callbacks.textile b/railties/guides/source/activerecord_validations_callbacks.textile
index 03d521ea1f..9d0ee29ff2 100644
--- a/railties/guides/source/activerecord_validations_callbacks.textile
+++ b/railties/guides/source/activerecord_validations_callbacks.textile
@@ -403,6 +403,47 @@ WARNING. Note that some databases are configured to perform case-insensitive sea
The default error message for +validates_uniqueness_of+ is "_has already been taken_".
+h4. +validates_with+
+
+This helper passes the record to a separate class for validation.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_with GoodnessValidator
+end
+
+class GoodnessValidator < ActiveRecord::Validator
+ def validate
+ if record.first_name == "Evil"
+ record.errors[:base] << "This person is evil"
+ end
+ end
+end
+</ruby>
+
+The +validates_with+ helper takes a class, or a list of classes to use for validation. There is no default error message for +validates_with+. You must manually add errors to the record's errors collection in the validator class.
+
+The validator class has two attributes by default:
+
+* +record+ - the record to be validated
+* +options+ - the extra options that were passed to +validates_with+
+
+Like all other validations, +validates_with+ takes the +:if+, +:unless+ and +:on+ options. If you pass any other options, it will send those options to the validator class as +options+:
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates_with GoodnessValidator, :fields => [:first_name, :last_name]
+end
+
+class GoodnessValidator < ActiveRecord::Validator
+ def validate
+ if options[:fields].any?{|field| record.send(field) == "Evil" }
+ record.errors[:base] << "This person is evil"
+ end
+ end
+end
+</ruby>
+
h4. +validates_each+
This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.