Module | ActiveRecord::Validations |
In: |
vendor/rails/activerecord/lib/active_record/validations.rb
|
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.
Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.
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 932 932: def save_with_validation(perform_validation = true) 933: if perform_validation && valid? || !perform_validation 934: save_without_validation 935: else 936: false 937: end 938: 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 942 942: def save_with_validation! 943: if valid? 944: save_without_validation! 945: else 946: raise RecordInvalid.new(self) 947: end 948: end
Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base 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 953 953: def update_attribute_with_validation_skipping(name, value) 954: send(name.to_s + '=', value) 955: save(false) 956: 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 959 959: def valid? 960: errors.clear 961: 962: run_validations(:validate) 963: validate 964: 965: if new_record? 966: run_validations(:validate_on_create) 967: validate_on_create 968: else 969: run_validations(:validate_on_update) 970: validate_on_update 971: end 972: 973: errors.empty? 974: 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 983 983: def validate #:doc: 984: end
Overwrite this method for validation checks used only on creation.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 987 987: def validate_on_create #:doc: 988: end