<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Class: ActiveRecord::Errors</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Class</strong></td> <td class="class-name-in-header">ActiveRecord::Errors</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../files/lib/active_record/validations_rb.html"> lib/active_record/validations.rb </a> <br /> </td> </tr> <tr class="top-aligned-row"> <td><strong>Parent:</strong></td> <td> Object </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Active Record validation is reported to and from this object, which is used by <a href="Base.html#M000524">Base#save</a> to determine whether the object is in a valid state to be saved. See usage example in <a href="Validations.html">Validations</a>. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000412">[]</a> <a href="#M000407">add</a> <a href="#M000409">add_on_blank</a> <a href="#M000408">add_on_empty</a> <a href="#M000406">add_to_base</a> <a href="#M000419">clear</a> <a href="#M000421">count</a> <a href="#M000405">default_error_messages</a> <a href="#M000414">each</a> <a href="#M000415">each_error</a> <a href="#M000416">each_full</a> <a href="#M000418">empty?</a> <a href="#M000417">full_messages</a> <a href="#M000424">generate_message</a> <a href="#M000410">invalid?</a> <a href="#M000422">length</a> <a href="#M000411">on</a> <a href="#M000413">on_base</a> <a href="#M000420">size</a> <a href="#M000423">to_xml</a> </div> </div> </div> <!-- if includes --> <div id="includes"> <h3 class="section-bar">Included Modules</h3> <div id="includes-list"> <span class="include-name">Enumerable</span> </div> </div> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Class methods</h3> <div id="method-M000405" class="method-detail"> <a name="M000405"></a> <div class="method-heading"> <a href="Errors.src/M000405.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000405.html');return false;"> <span class="method-name">default_error_messages</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000412" class="method-detail"> <a name="M000412"></a> <div class="method-heading"> <span class="method-name">[]</span><span class="method-args">(attribute)</span> </div> <div class="method-description"> <p> Alias for <a href="Errors.html#M000411">on</a> </p> </div> </div> <div id="method-M000407" class="method-detail"> <a name="M000407"></a> <div class="method-heading"> <a href="Errors.src/M000407.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000407.html');return false;"> <span class="method-name">add</span><span class="method-args">(error_or_attr, message = nil, options = {})</span> </a> </div> <div class="method-description"> <p> Adds an error message (<tt>messsage</tt>) to the <tt>attribute</tt>, which will be returned <a href="Errors.html#M000411">on</a> a call to <tt><a href="Errors.html#M000411">on</a>(attribute)</tt> for the same attribute and ensure that this error object returns false when asked if <tt><a href="Errors.html#M000418">empty?</a></tt>. More than one error can be added to the same <tt>attribute</tt> in which case an array will be returned <a href="Errors.html#M000411">on</a> a call to <tt><a href="Errors.html#M000411">on</a>(attribute)</tt>. If no <tt>messsage</tt> is supplied, :invalid is assumed. If <tt>message</tt> is a Symbol, it will be translated, using the appropriate scope (see translate_error). def <a href="Errors.html#M000407">add</a>(attribute, message = nil, options = {}) </p> <pre> message ||= :invalid message = generate_message(attribute, message, options)) if message.is_a?(Symbol) @errors[attribute.to_s] ||= [] @errors[attribute.to_s] << message </pre> <p> end </p> </div> </div> <div id="method-M000409" class="method-detail"> <a name="M000409"></a> <div class="method-heading"> <a href="Errors.src/M000409.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000409.html');return false;"> <span class="method-name">add_on_blank</span><span class="method-args">(attributes, custom_message = nil)</span> </a> </div> <div class="method-description"> <p> Will <a href="Errors.html#M000407">add</a> an error message to <a href="Errors.html#M000414">each</a> of the attributes in <tt>attributes</tt> that is blank (using Object#blank?). </p> </div> </div> <div id="method-M000408" class="method-detail"> <a name="M000408"></a> <div class="method-heading"> <a href="Errors.src/M000408.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000408.html');return false;"> <span class="method-name">add_on_empty</span><span class="method-args">(attributes, custom_message = nil)</span> </a> </div> <div class="method-description"> <p> Will <a href="Errors.html#M000407">add</a> an error message to <a href="Errors.html#M000414">each</a> of the attributes in <tt>attributes</tt> that is empty. </p> </div> </div> <div id="method-M000406" class="method-detail"> <a name="M000406"></a> <div class="method-heading"> <a href="Errors.src/M000406.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000406.html');return false;"> <span class="method-name">add_to_base</span><span class="method-args">(msg)</span> </a> </div> <div class="method-description"> <p> 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 <tt><a href="Errors.html#M000416">each_full</a></tt>, so they should be complete sentences. </p> </div> </div> <div id="method-M000419" class="method-detail"> <a name="M000419"></a> <div class="method-heading"> <a href="Errors.src/M000419.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000419.html');return false;"> <span class="method-name">clear</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Removes all errors that have been added. </p> </div> </div> <div id="method-M000421" class="method-detail"> <a name="M000421"></a> <div class="method-heading"> <span class="method-name">count</span><span class="method-args">()</span> </div> <div class="method-description"> <p> Alias for <a href="Errors.html#M000420">size</a> </p> </div> </div> <div id="method-M000414" class="method-detail"> <a name="M000414"></a> <div class="method-heading"> <a href="Errors.src/M000414.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000414.html');return false;"> <span class="method-name">each</span><span class="method-args">() {|attr, error.message| ...}</span> </a> </div> <div class="method-description"> <p> Yields <a href="Errors.html#M000414">each</a> attribute and associated message per error added. </p> <pre> 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 </pre> </div> </div> <div id="method-M000415" class="method-detail"> <a name="M000415"></a> <div class="method-heading"> <a href="Errors.src/M000415.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000415.html');return false;"> <span class="method-name">each_error</span><span class="method-args">() {|attr, error| ...}</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000416" class="method-detail"> <a name="M000416"></a> <div class="method-heading"> <a href="Errors.src/M000416.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000416.html');return false;"> <span class="method-name">each_full</span><span class="method-args">() {|msg| ...}</span> </a> </div> <div class="method-description"> <p> Yields <a href="Errors.html#M000414">each</a> full error message added. So <tt>Person.errors.<a href="Errors.html#M000407">add</a>("first_name", "can‘t be empty")</tt> will be returned through iteration as "First name can‘t be empty". </p> <pre> 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 </pre> </div> </div> <div id="method-M000418" class="method-detail"> <a name="M000418"></a> <div class="method-heading"> <a href="Errors.src/M000418.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000418.html');return false;"> <span class="method-name">empty?</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns true if no errors have been added. </p> </div> </div> <div id="method-M000417" class="method-detail"> <a name="M000417"></a> <div class="method-heading"> <a href="Errors.src/M000417.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000417.html');return false;"> <span class="method-name">full_messages</span><span class="method-args">(options = {})</span> </a> </div> <div class="method-description"> <p> Returns all the full error messages in an array. </p> <pre> 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"] </pre> </div> </div> <div id="method-M000424" class="method-detail"> <a name="M000424"></a> <div class="method-heading"> <a href="Errors.src/M000424.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000424.html');return false;"> <span class="method-name">generate_message</span><span class="method-args">(attribute, message = :invalid, options = {})</span> </a> </div> <div class="method-description"> </div> </div> <div id="method-M000410" class="method-detail"> <a name="M000410"></a> <div class="method-heading"> <a href="Errors.src/M000410.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000410.html');return false;"> <span class="method-name">invalid?</span><span class="method-args">(attribute)</span> </a> </div> <div class="method-description"> <p> Returns true if the specified <tt>attribute</tt> has errors associated with it. </p> <pre> 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 </pre> </div> </div> <div id="method-M000422" class="method-detail"> <a name="M000422"></a> <div class="method-heading"> <span class="method-name">length</span><span class="method-args">()</span> </div> <div class="method-description"> <p> Alias for <a href="Errors.html#M000420">size</a> </p> </div> </div> <div id="method-M000411" class="method-detail"> <a name="M000411"></a> <div class="method-heading"> <a href="Errors.src/M000411.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000411.html');return false;"> <span class="method-name">on</span><span class="method-args">(attribute)</span> </a> </div> <div class="method-description"> <p> Returns <tt>nil</tt>, if no errors are associated with the specified <tt>attribute</tt>. Returns the error message, if one error is associated with the specified <tt>attribute</tt>. Returns an array of error messages, if more than one error is associated with the specified <tt>attribute</tt>. </p> <pre> 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 </pre> </div> </div> <div id="method-M000413" class="method-detail"> <a name="M000413"></a> <div class="method-heading"> <a href="Errors.src/M000413.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000413.html');return false;"> <span class="method-name">on_base</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns errors assigned to the base object through <tt><a href="Errors.html#M000406">add_to_base</a></tt> according to the normal rules of <tt><a href="Errors.html#M000411">on</a>(attribute)</tt>. </p> </div> </div> <div id="method-M000420" class="method-detail"> <a name="M000420"></a> <div class="method-heading"> <a href="Errors.src/M000420.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000420.html');return false;"> <span class="method-name">size</span><span class="method-args">()</span> </a> </div> <div class="method-description"> <p> Returns the total number of errors added. Two errors added to the same attribute will be counted as such. </p> </div> </div> <div id="method-M000423" class="method-detail"> <a name="M000423"></a> <div class="method-heading"> <a href="Errors.src/M000423.html" target="Code" class="method-signature" onclick="popupCode('Errors.src/M000423.html');return false;"> <span class="method-name">to_xml</span><span class="method-args">(options={})</span> </a> </div> <div class="method-description"> <p> Returns an XML representation of this error object. </p> <pre> 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> </pre> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>