Skip to content
This repository
Browse code

Generate strict validation error messages with attribute name

  • Loading branch information...
commit 26861e95064c57159486a02709ca0f2436596062 1 parent 0b5edb3
Carlos Antonio da Silva authored February 01, 2012
2  activemodel/lib/active_model/errors.rb
@@ -224,7 +224,7 @@ def to_hash
224 224
     def add(attribute, message = nil, options = {})
225 225
       message = normalize_message(attribute, message, options)
226 226
       if options[:strict]
227  
-        raise ActiveModel::StrictValidationFailed,  message
  227
+        raise ActiveModel::StrictValidationFailed, full_message(attribute, message)
228 228
       end
229 229
 
230 230
       self[attribute] << message
13  activemodel/lib/active_model/validations/validates.rb
... ...
@@ -1,7 +1,6 @@
1 1
 require 'active_support/core_ext/hash/slice'
2 2
 
3 3
 module ActiveModel
4  
-
5 4
   # == Active Model validates method
6 5
   module Validations
7 6
     module ClassMethods
@@ -101,11 +100,11 @@ def validates(*attributes)
101 100
         end
102 101
       end
103 102
 
104  
-      # This method is used to define validation that can not be corrected by end user
105  
-      # and is considered exceptional.
106  
-      # So each validator defined with bang or <tt>:strict</tt> option set to <tt>true</tt>
107  
-      # will always raise <tt>ActiveModel::InternalValidationFailed</tt> instead of adding error
108  
-      # when validation fails
  103
+      # This method is used to define validation that cannot be corrected by end
  104
+      # user and is considered exceptional. So each validator defined with bang
  105
+      # or <tt>:strict</tt> option set to <tt>true</tt> will always raise
  106
+      # <tt>ActiveModel::StrictValidationFailed</tt> instead of adding error
  107
+      # when validation fails.
109 108
       # See <tt>validates</tt> for more information about validation itself.
110 109
       def validates!(*attributes)
111 110
         options = attributes.extract_options!
@@ -118,7 +117,7 @@ def validates!(*attributes)
118 117
       # When creating custom validators, it might be useful to be able to specify
119 118
       # additional default keys. This can be done by overwriting this method.
120 119
       def _validates_default_keys
121  
-        [ :if, :unless, :on, :allow_blank, :allow_nil , :strict]
  120
+        [:if, :unless, :on, :allow_blank, :allow_nil , :strict]
122 121
       end
123 122
 
124 123
       def _parse_validates_options(options) #:nodoc:
15  activemodel/test/cases/validations_test.rb
@@ -310,7 +310,7 @@ def test_strict_validation_not_fails
310 310
   end
311 311
 
312 312
   def test_strict_validation_particular_validator
313  
-    Topic.validates :title,  :presence => {:strict => true}
  313
+    Topic.validates :title,  :presence => { :strict => true }
314 314
     assert_raises ActiveModel::StrictValidationFailed do
315 315
       Topic.new.valid?
316 316
     end
@@ -330,9 +330,18 @@ def test_validates_with_bang
330 330
     end
331 331
   end
332 332
 
  333
+  def test_strict_validation_error_message
  334
+    Topic.validates :title, :strict => true, :presence => true
  335
+
  336
+    exception = assert_raises(ActiveModel::StrictValidationFailed) do
  337
+      Topic.new.valid?
  338
+    end
  339
+    assert_equal "Title can't be blank", exception.message
  340
+  end
  341
+
333 342
   def test_does_not_modify_options_argument
334  
-    options = {:presence => true}
  343
+    options = { :presence => true }
335 344
     Topic.validates :title, options
336  
-    assert_equal({:presence => true}, options)
  345
+    assert_equal({ :presence => true }, options)
337 346
   end
338 347
 end

0 notes on commit 26861e9

Please sign in to comment.
Something went wrong with that request. Please try again.