aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/validations/length.rb12
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb14
2 files changed, 22 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index 3e76796355..e91841bd1c 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -66,10 +66,14 @@ module ActiveModel
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
- if value.nil? or value.size < option_value.begin
- record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => option_value.begin)
- elsif value.size > option_value.end
- record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => option_value.end)
+
+ min, max = option_value.begin, option_value.end
+ max = max - 1 if option_value.exclude_end?
+
+ if value.nil? || value.size < min
+ record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => min)
+ elsif value.size > max
+ record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => max)
end
end
when :is, :minimum, :maximum
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index 499f6a5e31..2c97b762f1 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -112,6 +112,20 @@ class LengthValidationTest < ActiveModel::TestCase
assert t.valid?
end
+ def test_validates_length_of_using_within_with_exclusive_range
+ Topic.validates_length_of(:title, :within => 4...10)
+
+ t = Topic.new("title" => "9 chars!!")
+ assert t.valid?
+
+ t.title = "Now I'm 10"
+ assert !t.valid?
+ assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]
+
+ t.title = "Four"
+ assert t.valid?
+ end
+
def test_optionally_validates_length_of_using_within
Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true