Home
Ruby on Rails
Models
Alias for size
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
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
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"]
Returns true if no errors have been added.
Alias for size
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
This method is also aliased as the shortcut [] (see below)
Returns errors that have been assigned to the base object through add_to_base according to the normal rules of on(attribute).
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
Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
Returns an XML representation of this error object.
Alias for on method...
company.errors[:email]
(attribute, msg = @@default_error_messages[:invalid])Adds an error message msg 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 msg is supplied, "invalid" is assumed.
([attributes], msg = @@default_error_messages[:blank])Will add an error message to each of the attributes in [attributes] that is blank (for example, an empty string).
(attributes, msg = @@default_error_messages[:empty])Will add an error message to each of the attributes in attributes that is empty.
(attributes, msg = @@default_error_messages[:empty])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.
Removes all the errors that have been added to the object.
These error messages are stored in a Rails class variable, @@default_error_messages and can be changed or added to as follows:
ActiveRecord::Errors.default_error_messages[:blank] = "Your custom message here"
These default error messages are used by Rails' built in validation class methods and some of the errors object's write methods such as add_on_blank. You may find it useful to change them if, for example, you require your error messages in a different language.
|
|
"is not included in the list" |
|
|
"is reserved" |
|
|
"is invalid" |
|
|
"doesn't match confirmation" |
|
|
"must be accepted" |
|
|
"can't be empty" |
|
|
"can't be blank" |
|
|
"is too long (maximum is %d characters)" |
|
|
"is too short (maximum is %d characters)" |
|
|
"is the wrong length (should be %d characters)" |
|
|
"has already been taken |
|
|
"is not a number |
|
|
"must be greater than %d" |
|
|
"must be greater than or equal to %d" |
|
|
"must be equal to %d" |
|
|
"must be less than %d" |
|
|
"must be less than or equal to %d" |
|
|
"must be odd" |
|
|
"must be even" |
(object, attribute, prepend_text = "", append_text = "", css_class = "formError")Returns a string containing the error message attached to the attribute of the object if one exists. This error message is wrapped in a <div> tag, which can be extended to include a prepend_text and/or append_text (to properly explain the error), and a css_class to style it accordingly. Object should either be the name of an instance variable or the actual object itself. As an example, let's say you have a model @post that has an error message on the title attribute...
error_message_on "post", "title" # => <div class="formError">can't be empty</div> error_message_on @post, "title" # => <div class="formError">can't be empty</div> error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" # => <div class="inputError">Title simply can't be empty (or it won't work).</div>
|
|
the name of an |
|
|
the attribute you wish to check for errors |
|
|
text to be prepended to the error message |
|
|
text to be appended to the error message |
|
|
CSS class of the |
({hash})Returns a string with a <div> containing all of the error messages for the objects located as instance variables by the names given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are provided.
This <div> can be tailored by the following options...
|
|
Used for the header of the error |
|
|
The class of the error |
|
|
The |
|
|
The object (or array of objects) for which to display errors, if you need to escape the instance variable convention |
|
|
The object name to use in the header, or any text that you prefer. If |
|
|
The message in the header of the error |
|
|
The explanation message after the header message and before the error list. Pass |
@user model...
error_messages_for :user
To specify more than one object, you simply list them: optionally, you can add an extra :object_name parameter, which will be the name used in the header message:
error_messages_for :user_common, :user, :object_name => :user
If the objects cannot be located as instance variables, you can add an extra :object parameter which gives the actual object (or array of objects to use)...
error_messages_for :user, :object => @question.user
This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors instance yourself and set it up.
Add new comment