validates_length_of with maximum should allow nil [#2309 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
jzw 2009-08-05 20:17:59 -05:00 committed by Pratik Naik
parent c34d6279a0
commit 5ab94b2595
2 changed files with 11 additions and 5 deletions

@ -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

@ -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