| Module | ActiveRecord::Validations |
| In: |
vendor/rails/activerecord/lib/active_record/validations.rb
|
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.
| VALIDATIONS | = | %w( validate validate_on_create validate_on_update ) |
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.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 1007
1007: def save_with_validation(perform_validation = true)
1008: if perform_validation && valid? || !perform_validation
1009: save_without_validation
1010: else
1011: false
1012: end
1013: 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.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 1017
1017: def save_with_validation!
1018: if valid?
1019: save_without_validation!
1020: else
1021: raise RecordInvalid.new(self)
1022: end
1023: end
Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 1026
1026: def valid?
1027: errors.clear
1028:
1029: run_callbacks(:validate)
1030: validate
1031:
1032: if new_record?
1033: run_callbacks(:validate_on_create)
1034: validate_on_create
1035: else
1036: run_callbacks(:validate_on_update)
1037: validate_on_update
1038: end
1039:
1040: errors.empty?
1041: end
Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 1050
1050: def validate #:doc:
1051: end
Overwrite this method for validation checks used only on creation.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 1054
1054: def validate_on_create #:doc:
1055: end