Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #4822 from carlosantoniodasilva/strict-validation-3-2

Generate strict validation error messages with attribute name (3-2-stable)
  • Loading branch information...
commit 0696a514bc5809cb77a6bd730277fd776e3f4475 2 parents 21735d2 + 91f8cf2
José Valim authored February 01, 2012
2  activemodel/lib/active_model/errors.rb
@@ -233,7 +233,7 @@ def to_hash
233 233
     def add(attribute, message = nil, options = {})
234 234
       message = normalize_message(attribute, message, options)
235 235
       if options[:strict]
236  
-        raise ActiveModel::StrictValidationFailed,  message
  236
+        raise ActiveModel::StrictValidationFailed, full_message(attribute, message)
237 237
       end
238 238
 
239 239
       self[attribute] << message
17  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
@@ -59,7 +58,7 @@ module ClassMethods
59 58
       #
60 59
       #   validates :name, :'film/title' => true
61 60
       #
62  
-      # The validators hash can also handle regular expressions, ranges, 
  61
+      # The validators hash can also handle regular expressions, ranges,
63 62
       # arrays and strings in shortcut form, e.g.
64 63
       #
65 64
       #   validates :email, :format => /@/
@@ -70,7 +69,7 @@ module ClassMethods
70 69
       # validator's initializer as +options[:in]+ while other types including
71 70
       # regular expressions and strings are passed as +options[:with]+
72 71
       #
73  
-      # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+ 
  72
+      # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+
74 73
       # can be given to one specific validator, as a hash:
75 74
       #
76 75
       #   validates :password, :presence => { :if => :password_required? }, :confirmation => true
@@ -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:
11  activemodel/test/cases/validations_test.rb
@@ -311,7 +311,7 @@ def test_strict_validation_not_fails
311 311
   end
312 312
 
313 313
   def test_strict_validation_particular_validator
314  
-    Topic.validates :title,  :presence => {:strict => true}
  314
+    Topic.validates :title,  :presence => { :strict => true }
315 315
     assert_raises ActiveModel::StrictValidationFailed do
316 316
       Topic.new.valid?
317 317
     end
@@ -330,4 +330,13 @@ def test_validates_with_bang
330 330
       Topic.new.valid?
331 331
     end
332 332
   end
  333
+
  334
+  def test_strict_validation_error_message
  335
+    Topic.validates :title, :strict => true, :presence => true
  336
+
  337
+    exception = assert_raises(ActiveModel::StrictValidationFailed) do
  338
+      Topic.new.valid?
  339
+    end
  340
+    assert_equal "Title can't be blank", exception.message
  341
+  end
333 342
 end

0 notes on commit 0696a51

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