Skip to content
Browse files

validates_length_of with maximum should allow nil [#2309 status:resol…

…ved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent c34d627 commit 5ab94b2595836fe2de36fd632ba9577c459b1292 @jzw jzw committed with lifo Aug 5, 2009
View
6 activemodel/lib/active_model/validations/length.rb
@@ -80,8 +80,10 @@ def validates_length_of(*attrs)
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
- unless !value.nil? and value.size.method(validity_checks[option])[option_value]
- record.errors.add(attr, key, :default => custom_message, :count => option_value)
+ unless option == :maximum and value.nil?
+ unless !value.nil? and value.size.send(validity_checks[option], option_value)
+ record.errors.add(attr, key, :default => custom_message, :count => option_value)
+ end
end
end
end
View
10 activemodel/test/cases/validations/length_validation_test.rb
@@ -52,6 +52,13 @@ def test_validates_length_of_using_minimum
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
end
+ def test_validates_length_of_using_maximum_should_allow_nil
+ Topic.validates_length_of :title, :maximum => 10
+ t = Topic.create
+ puts t.errors
+ assert t.valid?
+ end
+
def test_optionally_validates_length_of_using_minimum
Topic.validates_length_of :title, :minimum => 5, :allow_nil => true
@@ -75,9 +82,6 @@ def test_validates_length_of_using_maximum
t.title = ""
assert t.valid?
-
- t.title = nil
- assert !t.valid?
end
def test_optionally_validates_length_of_using_maximum

0 comments on commit 5ab94b2

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