1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

revised some conventions in validations docs

This commit is contained in:
Xavier Noria 2008-05-25 20:51:18 +02:00
parent 80bc36d808
commit 9d9c96af56

View file

@ -1,5 +1,5 @@
module ActiveRecord module ActiveRecord
# Raised by save! and create! when the record is invalid. Use the # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
# +record+ method to retrieve the record which did not validate. # +record+ method to retrieve the record which did not validate.
# begin # begin
# complex_operation_that_calls_save!_internally # complex_operation_that_calls_save!_internally
@ -52,7 +52,7 @@ module ActiveRecord
# Adds an error to the base object instead of any particular attribute. This is used # 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 # 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 # 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. # with +each_full+, so they should be complete sentences.
def add_to_base(msg) def add_to_base(msg)
add(:base, msg) add(:base, msg)
end end
@ -97,7 +97,7 @@ module ActiveRecord
!@errors[attribute.to_s].nil? !@errors[attribute.to_s].nil?
end end
# Returns nil, if no errors are associated with the specified +attribute+. # 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 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+. # Returns an array of error messages, if more than one error is associated with the specified +attribute+.
# #
@ -118,7 +118,7 @@ module ActiveRecord
alias :[] :on alias :[] :on
# Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute). # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
def on_base def on_base
on(:base) on(:base)
end end
@ -131,15 +131,15 @@ module ActiveRecord
# end # end
# #
# company = Company.create(:address => '123 First St.') # company = Company.create(:address => '123 First St.')
# company.errors.each{|attr,msg| puts "#{attr} - #{msg}" } # => # company.errors.each{|attr,msg| puts "#{attr} - #{msg}" }
# name - is too short (minimum is 5 characters) # # => name - is too short (minimum is 5 characters)
# name - can't be blank # # name - can't be blank
# address - can't be blank # # address - can't be blank
def each def each
@errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
end end
# Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned # Yields each full error message added. So <tt>Person.errors.add("first_name", "can't be empty")</tt> will be returned
# through iteration as "First name can't be empty". # through iteration as "First name can't be empty".
# #
# class Company < ActiveRecord::Base # class Company < ActiveRecord::Base
@ -148,10 +148,10 @@ module ActiveRecord
# end # end
# #
# company = Company.create(:address => '123 First St.') # company = Company.create(:address => '123 First St.')
# company.errors.each_full{|msg| puts msg } # => # company.errors.each_full{|msg| puts msg }
# Name is too short (minimum is 5 characters) # # => Name is too short (minimum is 5 characters)
# Name can't be blank # # Name can't be blank
# Address can't be blank # # Address can't be blank
def each_full def each_full
full_messages.each { |msg| yield msg } full_messages.each { |msg| yield msg }
end end
@ -164,8 +164,8 @@ module ActiveRecord
# end # end
# #
# company = Company.create(:address => '123 First St.') # company = Company.create(:address => '123 First St.')
# company.errors.full_messages # => # company.errors.full_messages
# ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"] # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
def full_messages def full_messages
full_messages = [] full_messages = []
@ -209,13 +209,13 @@ module ActiveRecord
# end # end
# #
# company = Company.create(:address => '123 First St.') # company = Company.create(:address => '123 First St.')
# company.errors.to_xml # => # company.errors.to_xml
# <?xml version="1.0" encoding="UTF-8"?> # # => <?xml version="1.0" encoding="UTF-8"?>
# <errors> # # <errors>
# <error>Name is too short (minimum is 5 characters)</error> # # <error>Name is too short (minimum is 5 characters)</error>
# <error>Name can't be blank</error> # # <error>Name can't be blank</error>
# <error>Address can't be blank</error> # # <error>Address can't be blank</error>
# </errors> # # </errors>
def to_xml(options={}) def to_xml(options={})
options[:root] ||= "errors" options[:root] ||= "errors"
options[:indent] ||= 2 options[:indent] ||= 2
@ -261,7 +261,7 @@ module ActiveRecord
# person.errors.on "phone_number" # => "has invalid format" # person.errors.on "phone_number" # => "has invalid format"
# person.errors.each_full { |msg| puts msg } # person.errors.each_full { |msg| puts msg }
# # => "Last name can't be empty\n" + # # => "Last name can't be empty\n" +
# "Phone number has invalid format" # # "Phone number has invalid format"
# #
# person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" } # person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" }
# person.save # => true (and person is now saved in the database) # person.save # => true (and person is now saved in the database)