diff options
author | Rodrigo Navarro <rnavarro1@gmail.com> | 2011-02-23 10:22:03 -0800 |
---|---|---|
committer | Rodrigo Navarro <rnavarro1@gmail.com> | 2011-02-23 10:22:03 -0800 |
commit | 29399e00c9a0875b6823989c636058910fd7c86b (patch) | |
tree | 5f507495e6acb85ceb414df9d8af4d7a9498000b /railties/guides/source | |
parent | f8cfa454e540def354c7ad3db62e7995165ea08b (diff) | |
download | rails-29399e00c9a0875b6823989c636058910fd7c86b.tar.gz rails-29399e00c9a0875b6823989c636058910fd7c86b.tar.bz2 rails-29399e00c9a0875b6823989c636058910fd7c86b.zip |
ActiveModel::Validator#validate must receive have a record parameter.
Diffstat (limited to 'railties/guides/source')
-rw-r--r-- | railties/guides/source/active_record_validations_callbacks.textile | 9 |
1 files changed, 3 insertions, 6 deletions
diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile index ea8cf8afaf..d27ba39104 100644 --- a/railties/guides/source/active_record_validations_callbacks.textile +++ b/railties/guides/source/active_record_validations_callbacks.textile @@ -417,7 +417,7 @@ class Person < ActiveRecord::Base end class GoodnessValidator < ActiveModel::Validator - def validate + def validate(record) if record.first_name == "Evil" record.errors[:base] << "This person is evil" end @@ -427,10 +427,7 @@ end 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+ +To implement the validate method, you must have an +record+ parameter defined, which is the record to be validated. 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+: @@ -440,7 +437,7 @@ class Person < ActiveRecord::Base end class GoodnessValidator < ActiveRecord::Validator - def validate + def validate(record) if options[:fields].any?{|field| record.send(field) == "Evil" } record.errors[:base] << "This person is evil" end |