Class | ActiveRecord::Errors |
In: |
vendor/rails/activerecord/lib/active_record/validations.rb
|
Parent: | Object |
Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object is in a valid state to be saved. See usage example in Validations.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 135 135: def default_error_messages 136: ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').") 137: I18n.translate 'activerecord.errors.messages' 138: end
Adds an error message (messsage) to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute in which case an array will be returned on a call to on(attribute). If no messsage is supplied, :invalid is assumed. If message is a Symbol, it will be translated, using the appropriate scope (see translate_error).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 160 160: def add(attribute, message = nil, options = {}) 161: options[:message] = options.delete(:default) if options.has_key?(:default) 162: error, message = message, nil if message.is_a?(Error) 163: 164: @errors[attribute.to_s] ||= [] 165: @errors[attribute.to_s] << (error || Error.new(@base, attribute, message, options)) 166: end
Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 178 178: def add_on_blank(attributes, custom_message = nil) 179: for attr in [attributes].flatten 180: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 181: add(attr, :blank, :default => custom_message) if value.blank? 182: end 183: end
Will add an error message to each of the attributes in attributes that is empty.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 169 169: def add_on_empty(attributes, custom_message = nil) 170: for attr in [attributes].flatten 171: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 172: is_empty = value.respond_to?(:empty?) ? value.empty? : false 173: add(attr, :empty, :default => custom_message) unless !value.nil? && !is_empty 174: end 175: end
Adds an error to the base object instead of any particular attribute. This is used to report errors that don‘t tie to any specific attribute, but rather to the object as a whole. These error messages don‘t get prepended with any field name when iterating with each_full, so they should be complete sentences.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 150 150: def add_to_base(msg) 151: add(:base, msg) 152: end
Removes all errors that have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 285 285: def clear 286: @errors = ActiveSupport::OrderedHash.new 287: end
Yields each attribute and associated message per error added.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.each{|attr,msg| puts "#{attr} - #{msg}" } # => name - is too short (minimum is 5 characters) # name - can't be blank # address - can't be blank
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 238 238: def each 239: @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error.message } } 240: end
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 242 242: def each_error 243: @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } } 244: end
Yields each full error message added. So Person.errors.add("first_name", "can‘t be empty") will be returned through iteration as "First name can‘t be empty".
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.each_full{|msg| puts msg } # => Name is too short (minimum is 5 characters) # Name can't be blank # Address can't be blank
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 259 259: def each_full 260: full_messages.each { |msg| yield msg } 261: end
Returns true if no errors have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 280 280: def empty? 281: @errors.empty? 282: end
Returns all the full error messages in an array.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.full_messages # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 273 273: def full_messages(options = {}) 274: @errors.values.inject([]) do |full_messages, errors| 275: full_messages + errors.map { |error| error.full_message } 276: end 277: end
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 323 323: def generate_message(attribute, message = :invalid, options = {}) 324: ActiveSupport::Deprecation.warn("ActiveRecord::Errors#generate_message has been deprecated. Please use ActiveRecord::Error.new().to_s.") 325: Error.new(@base, attribute, message, options).to_s 326: end
Returns true if the specified attribute has errors associated with it.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.invalid?(:name) # => true company.errors.invalid?(:address) # => false
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 195 195: def invalid?(attribute) 196: !@errors[attribute.to_s].nil? 197: end
Returns nil, if no errors are associated with the specified attribute. Returns the error message, if one error is associated with the specified attribute. Returns an array of error messages, if more than one error is associated with the specified attribute.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.on(:name) # => ["is too short (minimum is 5 characters)", "can't be blank"] company.errors.on(:email) # => "can't be blank" company.errors.on(:address) # => nil
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 212 212: def on(attribute) 213: attribute = attribute.to_s 214: return nil unless @errors.has_key?(attribute) 215: errors = @errors[attribute].map(&:to_s) 216: errors.size == 1 ? errors.first : errors 217: end
Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 222 222: def on_base 223: on(:base) 224: end
Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 290 290: def size 291: @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } 292: end
Returns an XML representation of this error object.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.to_xml # => <?xml version="1.0" encoding="UTF-8"?> # <errors> # <error>Name is too short (minimum is 5 characters)</error> # <error>Name can't be blank</error> # <error>Address can't be blank</error> # </errors>
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 312 312: def to_xml(options={}) 313: options[:root] ||= "errors" 314: options[:indent] ||= 2 315: options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 316: 317: options[:builder].instruct! unless options.delete(:skip_instruct) 318: options[:builder].errors do |e| 319: full_messages.each { |msg| e.error(msg) } 320: end 321: end