Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.
Active Records implement validation by overwriting Base#validate (or the variations, validate_on_create and validate_on_update). Each of these methods can inspect the state of the object, which usually means ensuring that a number of attributes have a certain value (such as not empty, within a given range, matching a certain regular expression).
Example:
class Person < ActiveRecord::Base protected def validate errors.add_on_empty %w( first_name last_name ) errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/ end def validate_on_create # is only run the first time a new object is saved unless valid_discount?(membership_discount) errors.add("membership_discount", "has expired") end end def validate_on_update errors.add_to_base("No changes have occurred") if unchanged_attributes? end end person = Person.new("first_name" => "David", "phone_number" => "what?") person.save # => false (and doesn't do the save) person.errors.empty? # => false person.errors.count # => 2 person.errors.on "last_name" # => "can't be empty" person.errors.on "phone_number" # => "has invalid format" person.errors.each_full { |msg| puts msg } # => "Last name can't be empty\n" + # "Phone number has invalid format" person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" } person.save # => true (and person is now saved in the database)
An Errors object is automatically created for every Active Record.
- errors
- invalid?
- save_with_validation
- save_with_validation!
- valid?
- validate
- validate_on_create
- validate_on_update
VALIDATIONS | = | %w( validate validate_on_create validate_on_update ) |
Returns the Errors object that holds all information about attribute error messages.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1129 1129: def errors 1130: @errors ||= Errors.new(self) 1131: end
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1124 1124: def invalid? 1125: !valid? 1126: end
The validation process on save can be skipped by passing false. The regular Base#save method is replaced with this when the validations module is mixed in, which it is by default.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1087 1087: def save_with_validation(perform_validation = true) 1088: if perform_validation && valid? || !perform_validation 1089: save_without_validation 1090: else 1091: false 1092: end 1093: end
Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false if the record is not valid.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1097 1097: def save_with_validation! 1098: if valid? 1099: save_without_validation! 1100: else 1101: raise RecordInvalid.new(self) 1102: end 1103: end
Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1106 1106: def valid? 1107: errors.clear 1108: 1109: run_callbacks(:validate) 1110: validate 1111: 1112: if new_record? 1113: run_callbacks(:validate_on_create) 1114: validate_on_create 1115: else 1116: run_callbacks(:validate_on_update) 1117: validate_on_update 1118: end 1119: 1120: errors.empty? 1121: end
Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1135 1135: def validate 1136: end
Overwrite this method for validation checks used only on creation.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1139 1139: def validate_on_create 1140: end
Overwrite this method for validation checks used only on updates.
[ show source ]
# File activerecord/lib/active_record/validations.rb, line 1143 1143: def validate_on_update 1144: end