Changeset 7098 for trunk/activeresource/lib/active_resource/validations.rb
- Timestamp:
- 06/23/07 17:29:54 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activeresource/lib/active_resource/validations.rb
r5962 r7098 15 15 end 16 16 17 # Add an error to the base Active Resource object rather than an attribute. 18 # 19 # ==== Examples 20 # my_folder = Folder.find(1) 21 # my_folder.errors.add_to_base("You can't edit an existing folder") 22 # my_folder.errors.on_base 23 # # => "You can't edit an existing folder" 24 # 25 # my_folder.errors.add_to_base("This folder has been tagged as frozen") 26 # my_folder.valid? 27 # # => false 28 # my_folder.errors.on_base 29 # # => ["You can't edit an existing folder", "This folder has been tagged as frozen"] 30 # 17 31 def add_to_base(msg) 18 32 add(:base, msg) 19 33 end 20 34 35 # Adds an error to an Active Resource object's attribute (named for the +attribute+ parameter) 36 # with the error message in +msg+. 37 # 38 # ==== Examples 39 # my_resource = Node.find(1) 40 # my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base' 41 # my_resource.errors.on('name') 42 # # => 'can not be "base"!' 43 # 44 # my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == '' 45 # my_resource.valid? 46 # # => false 47 # my_resource.errors.on('desc') 48 # # => 'can not be blank!' 49 # 21 50 def add(attribute, msg) 22 51 @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? … … 25 54 26 55 # Returns true if the specified +attribute+ has errors associated with it. 56 # 57 # ==== Examples 58 # my_resource = Disk.find(1) 59 # my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main' 60 # my_resource.errors.on('location') 61 # # => 'must be Main!' 62 # 63 # my_resource.errors.invalid?('location') 64 # # => true 65 # my_resource.errors.invalid?('name') 66 # # => false 27 67 def invalid?(attribute) 28 68 !@errors[attribute.to_s].nil? 29 69 end 30 70 31 # * Returns nil, if no errors are associated with the specified +attribute+. 32 # * Returns the error message, if one error is associated with the specified +attribute+. 33 # * Returns an array of error messages, if more than one error is associated with the specified +attribute+. 71 # A method to return the errors associated with +attribute+, which returns nil, if no errors are 72 # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, 73 # or an array of error messages if more than one error is associated with the specified +attribute+. 74 # 75 # ==== Examples 76 # my_person = Person.new(params[:person]) 77 # my_person.errors.on('login') 78 # # => nil 79 # 80 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 81 # my_person.errors.on('login') 82 # # => 'can not be empty' 83 # 84 # my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10 85 # my_person.errors.on('login') 86 # # => ['can not be empty', 'can not be longer than 10 characters'] 34 87 def on(attribute) 35 88 errors = @errors[attribute.to_s] … … 40 93 alias :[] :on 41 94 42 # Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute). 95 # A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are 96 # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, 97 # or an array of error messages if more than one error is associated with the specified +attribute+. 98 # 99 # ==== Examples 100 # my_account = Account.find(1) 101 # my_account.errors.on_base 102 # # => nil 103 # 104 # my_account.errors.add_to_base("This account is frozen") 105 # my_account.errors.on_base 106 # # => "This account is frozen" 107 # 108 # my_account.errors.add_to_base("This account has been closed") 109 # my_account.errors.on_base 110 # # => ["This account is frozen", "This account has been closed"] 111 # 43 112 def on_base 44 113 on(:base) … … 46 115 47 116 # Yields each attribute and associated message per error added. 117 # 118 # ==== Examples 119 # my_person = Person.new(params[:person]) 120 # 121 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 122 # my_person.errors.add('password', 'can not be empty') if my_person.password == '' 123 # messages = '' 124 # my_person.errors.each {|attr, msg| messages += attr.humanize + " " + msg + "<br />"} 125 # messages 126 # # => "Login can not be empty<br />Password can not be empty<br />" 127 # 48 128 def each 49 129 @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } … … 52 132 # Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned 53 133 # through iteration as "First name can't be empty". 134 # 135 # ==== Examples 136 # my_person = Person.new(params[:person]) 137 # 138 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 139 # my_person.errors.add('password', 'can not be empty') if my_person.password == '' 140 # messages = '' 141 # my_person.errors.each_full {|msg| messages += msg + "<br/>"} 142 # messages 143 # # => "Login can not be empty<br />Password can not be empty<br />" 144 # 54 145 def each_full 55 146 full_messages.each { |msg| yield msg } … … 57 148 58 149 # Returns all the full error messages in an array. 150 # 151 # ==== Examples 152 # my_person = Person.new(params[:person]) 153 # 154 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 155 # my_person.errors.add('password', 'can not be empty') if my_person.password == '' 156 # messages = '' 157 # my_person.errors.full_messages.each {|msg| messages += msg + "<br/>"} 158 # messages 159 # # => "Login can not be empty<br />Password can not be empty<br />" 160 # 59 161 def full_messages 60 162 full_messages = [] … … 80 182 # Returns the total number of errors added. Two errors added to the same attribute will be counted as such 81 183 # with this as well. 184 # 185 # ==== Examples 186 # my_person = Person.new(params[:person]) 187 # my_person.errors.size 188 # # => 0 189 # 190 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 191 # my_person.errors.add('password', 'can not be empty') if my_person.password == '' 192 # my_person.error.size 193 # # => 2 194 # 82 195 def size 83 196 @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } … … 87 200 alias_method :length, :size 88 201 202 # Grabs errors from the XML response. 89 203 def from_xml(xml) 90 204 clear … … 103 217 end 104 218 105 # Module to allow validation of ActiveResource objects, which are implemented by overriding +Base#validate+ or its variants. 106 # Each of these methods can inspect the state of the object, which usually means ensuring that a number of 107 # attributes have a certain value (such as not empty, within a given range, matching a certain regular expression). For example: 219 # Module to allow validation of ActiveResource objects, which creates an Errors instance for every resource. 220 # Methods are implemented by overriding +Base#validate+ or its variants Each of these methods can inspect 221 # the state of the object, which usually means ensuring that a number of attributes have a certain value 222 # (such as not empty, within a given range, matching a certain regular expression and so on). 223 # 224 # ==== Example 108 225 # 109 226 # class Person < ActiveResource::Base … … 134 251 # person.save # => true (and person is now saved to the remote service) 135 252 # 136 # An Errors object is automatically created for every resource.137 253 module Validations 138 254 def self.included(base) # :nodoc: … … 142 258 end 143 259 260 # Validate a resource and save (POST) it to the remote web service. 144 261 def save_with_validation 145 262 save_without_validation … … 150 267 end 151 268 269 # Checks for errors on an object (i.e., is resource.errors empty?). 270 # 271 # ==== Examples 272 # my_person = Person.create(params[:person]) 273 # my_person.valid? 274 # # => true 275 # 276 # my_person.errors.add('login', 'can not be empty') if my_person.login == '' 277 # my_person.valid? 278 # # => false 152 279 def valid? 153 280 errors.empty?