diff options
author | José Valim <jose.valim@gmail.com> | 2011-08-25 07:10:16 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-08-25 07:10:16 -0700 |
commit | d15069316f331c46165248c2df869145fef60008 (patch) | |
tree | b3565673bfd275620a41b50b1fbe86b4d798c4e3 /activemodel/lib/active_model/validations/validates.rb | |
parent | 68424fb1cf45acda3ad1daef30136f3bcd4f9f9d (diff) | |
parent | cbb147931b2f547ad76a7ed233430642490c6000 (diff) | |
download | rails-d15069316f331c46165248c2df869145fef60008.tar.gz rails-d15069316f331c46165248c2df869145fef60008.tar.bz2 rails-d15069316f331c46165248c2df869145fef60008.zip |
Merge pull request #2563 from bogdan/internal_validation
Implemented strict validation concept
Diffstat (limited to 'activemodel/lib/active_model/validations/validates.rb')
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 7ff42de00b..b85c2453fb 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -70,8 +70,8 @@ module ActiveModel # validator's initializer as +options[:in]+ while other types including # regular expressions and strings are passed as +options[:with]+ # - # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+ and +:allow_nil+ can be given - # to one specific validator, as a hash: + # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+ + # can be given to one specific validator, as a hash: # # validates :password, :presence => { :if => :password_required? }, :confirmation => true # @@ -101,12 +101,24 @@ module ActiveModel end end + # This method is used to define validation that can not be corrected by end user + # and is considered exceptional. + # So each validator defined with bang or <tt>:strict</tt> option set to <tt>true</tt> + # will always raise <tt>ActiveModel::InternalValidationFailed</tt> instead of adding error + # when validation fails + # See <tt>validates</tt> for more information about validation itself. + def validates!(*attributes) + options = attributes.extract_options! + options[:strict] = true + validates(*(attributes << options)) + end + protected # When creating custom validators, it might be useful to be able to specify # additional default keys. This can be done by overwriting this method. def _validates_default_keys - [ :if, :unless, :on, :allow_blank, :allow_nil ] + [ :if, :unless, :on, :allow_blank, :allow_nil , :strict] end def _parse_validates_options(options) #:nodoc: |