Active Resource validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.

Methods
Included Modules
Attributes
[R] errors
Public Instance methods
[](attribute)

Alias for on

add(attribute, msg)

Adds an error to an Active Resource object‘s attribute (named for the attribute parameter) with the error message in msg.

Examples

  my_resource = Node.find(1)
  my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base'
  my_resource.errors.on('name')
  # => 'can not be "base"!'

  my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == ''
  my_resource.valid?
  # => false
  my_resource.errors.on('desc')
  # => 'can not be blank!'
    # File activeresource/lib/active_resource/validations.rb, line 50
50:     def add(attribute, msg)
51:       @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
52:       @errors[attribute.to_s] << msg
53:     end
add_to_base(msg)

Add an error to the base Active Resource object rather than an attribute.

Examples

  my_folder = Folder.find(1)
  my_folder.errors.add_to_base("You can't edit an existing folder")
  my_folder.errors.on_base
  # => "You can't edit an existing folder"

  my_folder.errors.add_to_base("This folder has been tagged as frozen")
  my_folder.valid?
  # => false
  my_folder.errors.on_base
  # => ["You can't edit an existing folder", "This folder has been tagged as frozen"]
    # File activeresource/lib/active_resource/validations.rb, line 31
31:     def add_to_base(msg)
32:       add(:base, msg)
33:     end
clear()
     # File activeresource/lib/active_resource/validations.rb, line 178
178:     def clear
179:       @errors = {}
180:     end
count()

Alias for size

each() {|attr, msg| ...}

Yields each attribute and associated message per error added.

Examples

  my_person = Person.new(params[:person])

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.errors.add('password', 'can not be empty') if my_person.password == ''
  messages = ''
  my_person.errors.each {|attr, msg| messages += attr.humanize + " " + msg + "<br />"}
  messages
  # => "Login can not be empty<br />Password can not be empty<br />"
     # File activeresource/lib/active_resource/validations.rb, line 128
128:     def each
129:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
130:     end
each_full() {|msg| ...}

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".

Examples

  my_person = Person.new(params[:person])

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.errors.add('password', 'can not be empty') if my_person.password == ''
  messages = ''
  my_person.errors.each_full {|msg| messages += msg + "<br/>"}
  messages
  # => "Login can not be empty<br />Password can not be empty<br />"
     # File activeresource/lib/active_resource/validations.rb, line 145
145:     def each_full
146:       full_messages.each { |msg| yield msg }
147:     end
from_array(messages)

Grabs errors from an array of messages (like ActiveRecord::Validations)

     # File activeresource/lib/active_resource/validations.rb, line 203
203:     def from_array(messages)
204:       clear
205:       humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
206:       messages.each do |message|
207:         attr_message = humanized_attributes.keys.detect do |attr_name|
208:           if message[0, attr_name.size + 1] == "#{attr_name} "
209:             add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
210:           end
211:         end
212:         
213:         add_to_base message if attr_message.nil?
214:       end
215:     end
from_json(json)

Grabs errors from the json response.

     # File activeresource/lib/active_resource/validations.rb, line 218
218:     def from_json(json)
219:       array = ActiveSupport::JSON.decode(json)['errors'] rescue []
220:       from_array array
221:     end
from_xml(xml)

Grabs errors from the XML response.

     # File activeresource/lib/active_resource/validations.rb, line 224
224:     def from_xml(xml)
225:       array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
226:       from_array array
227:     end
full_messages()

Returns all the full error messages in an array.

Examples

  my_person = Person.new(params[:person])

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.errors.add('password', 'can not be empty') if my_person.password == ''
  messages = ''
  my_person.errors.full_messages.each {|msg| messages += msg + "<br/>"}
  messages
  # => "Login can not be empty<br />Password can not be empty<br />"
     # File activeresource/lib/active_resource/validations.rb, line 161
161:     def full_messages
162:       full_messages = []
163: 
164:       @errors.each_key do |attr|
165:         @errors[attr].each do |msg|
166:           next if msg.nil?
167: 
168:           if attr == "base"
169:             full_messages << msg
170:           else
171:             full_messages << [attr.humanize, msg].join(' ')
172:           end
173:         end
174:       end
175:       full_messages
176:     end
invalid?(attribute)

Returns true if the specified attribute has errors associated with it.

Examples

  my_resource = Disk.find(1)
  my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main'
  my_resource.errors.on('location')
  # => 'must be Main!'

  my_resource.errors.invalid?('location')
  # => true
  my_resource.errors.invalid?('name')
  # => false
    # File activeresource/lib/active_resource/validations.rb, line 67
67:     def invalid?(attribute)
68:       !@errors[attribute.to_s].nil?
69:     end
length()

Alias for size

on(attribute)

A method to return the errors associated with attribute, which returns nil, if no errors are associated with the specified attribute, the error message if one error is associated with the specified attribute, or an array of error messages if more than one error is associated with the specified attribute.

Examples

  my_person = Person.new(params[:person])
  my_person.errors.on('login')
  # => nil

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.errors.on('login')
  # => 'can not be empty'

  my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10
  my_person.errors.on('login')
  # => ['can not be empty', 'can not be longer than 10 characters']
This method is also aliased as []
    # File activeresource/lib/active_resource/validations.rb, line 87
87:     def on(attribute)
88:       errors = @errors[attribute.to_s]
89:       return nil if errors.nil?
90:       errors.size == 1 ? errors.first : errors
91:     end
on_base()

A method to return errors assigned to base object through add_to_base, which returns nil, if no errors are associated with the specified attribute, the error message if one error is associated with the specified attribute, or an array of error messages if more than one error is associated with the specified attribute.

Examples

  my_account = Account.find(1)
  my_account.errors.on_base
  # => nil

  my_account.errors.add_to_base("This account is frozen")
  my_account.errors.on_base
  # => "This account is frozen"

  my_account.errors.add_to_base("This account has been closed")
  my_account.errors.on_base
  # => ["This account is frozen", "This account has been closed"]
     # File activeresource/lib/active_resource/validations.rb, line 112
112:     def on_base
113:       on(:base)
114:     end
size()

Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.

Examples

  my_person = Person.new(params[:person])
  my_person.errors.size
  # => 0

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.errors.add('password', 'can not be empty') if my_person.password == ''
  my_person.error.size
  # => 2
This method is also aliased as count length
     # File activeresource/lib/active_resource/validations.rb, line 195
195:     def size
196:       @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
197:     end