Skip to content

Commit

Permalink
validates_length_of with maximum should allow nil [#2309 status:resol…
Browse files Browse the repository at this point in the history
…ved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
jzw authored and lifo committed Aug 8, 2009
1 parent c34d627 commit 5ab94b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions activemodel/lib/active_model/validations/length.rb
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions activemodel/test/cases/validations/length_validation_test.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 5ab94b2

Please sign in to comment.