aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations/exclusion.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-19 23:28:59 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-19 23:28:59 +0000
commit8828b2ca674acfa028a3c1e086a1795d3bb893e1 (patch)
tree17a0b5793d99e0c0a05f31c70adea642946a87a3 /activemodel/lib/active_model/validations/exclusion.rb
parent6ed42ebdff05f9d28a60e91093d8f9afad03a958 (diff)
downloadrails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.tar.gz
rails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.tar.bz2
rails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.zip
Move all the Active Record validations to Active Model
Diffstat (limited to 'activemodel/lib/active_model/validations/exclusion.rb')
-rw-r--r--activemodel/lib/active_model/validations/exclusion.rb16
1 files changed, 9 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb
index 1592000fc7..435ba5cab6 100644
--- a/activemodel/lib/active_model/validations/exclusion.rb
+++ b/activemodel/lib/active_model/validations/exclusion.rb
@@ -6,14 +6,14 @@ module ActiveModel
# class Person < ActiveRecord::Base
# validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
# validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
- # validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension %s is not allowed"
+ # validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension {{value}} is not allowed"
# end
#
# Configuration options:
- # * <tt>:in</tt> - An enumerable object of items that the value shouldn't be part of
- # * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved")
- # * <tt>:allow_nil</tt> - If set to +true+, skips this validation if the attribute is +nil+ (default is: +false+)
- # * <tt>:allow_blank</tt> - If set to +true+, skips this validation if the attribute is blank (default is: +false+)
+ # * <tt>:in</tt> - An enumerable object of items that the value shouldn't be part of.
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved").
+ # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
+ # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
@@ -21,7 +21,7 @@ module ActiveModel
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
def validates_exclusion_of(*attr_names)
- configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save }
+ configuration = { :on => :save }
configuration.update(attr_names.extract_options!)
enum = configuration[:in] || configuration[:within]
@@ -29,7 +29,9 @@ module ActiveModel
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?(:include?)
validates_each(attr_names, configuration) do |record, attr_name, value|
- record.errors.add(attr_name, configuration[:message] % value) if enum.include?(value)
+ if enum.include?(value)
+ record.errors.add(attr_name, :exclusion, :default => configuration[:message], :value => value)
+ end
end
end
end