mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Length validation handles correctly nil. Fix #7180
When nil or empty string are not allowed, they are not valid.
This commit is contained in:
parent
02c30c6426
commit
ea76e9a312
3 changed files with 57 additions and 2 deletions
|
@ -1,5 +1,9 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Fixed length validator to correctly handle nil values. Fixes #7180.
|
||||||
|
|
||||||
|
*Michal Zima*
|
||||||
|
|
||||||
* Use BCrypt's MIN_COST in the test environment for speedier tests when using `has_secure_pasword`.
|
* Use BCrypt's MIN_COST in the test environment for speedier tests when using `has_secure_pasword`.
|
||||||
|
|
||||||
*Brian Cardarella + Jeremy Kemper + Trevor Turk*
|
*Brian Cardarella + Jeremy Kemper + Trevor Turk*
|
||||||
|
|
|
@ -14,6 +14,10 @@ module ActiveModel
|
||||||
options[:minimum], options[:maximum] = range.min, range.max
|
options[:minimum], options[:maximum] = range.min, range.max
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if options[:allow_blank] == false && options[:minimum].nil? && options[:is].nil?
|
||||||
|
options[:minimum] = 1
|
||||||
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +44,10 @@ module ActiveModel
|
||||||
|
|
||||||
CHECKS.each do |key, validity_check|
|
CHECKS.each do |key, validity_check|
|
||||||
next unless check_value = options[key]
|
next unless check_value = options[key]
|
||||||
next if value_length.send(validity_check, check_value)
|
|
||||||
|
if !value.nil? || skip_nil_check?(key)
|
||||||
|
next if value_length.send(validity_check, check_value)
|
||||||
|
end
|
||||||
|
|
||||||
errors_options[:count] = check_value
|
errors_options[:count] = check_value
|
||||||
|
|
||||||
|
@ -58,6 +65,10 @@ module ActiveModel
|
||||||
options[:tokenizer].call(value)
|
options[:tokenizer].call(value)
|
||||||
end || value
|
end || value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def skip_nil_check?(key)
|
||||||
|
key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module HelperMethods
|
module HelperMethods
|
||||||
|
@ -79,7 +90,8 @@ module ActiveModel
|
||||||
#
|
#
|
||||||
# Configuration options:
|
# Configuration options:
|
||||||
# * <tt>:minimum</tt> - The minimum size of the attribute.
|
# * <tt>:minimum</tt> - The minimum size of the attribute.
|
||||||
# * <tt>:maximum</tt> - The maximum size of the attribute.
|
# * <tt>:maximum</tt> - The maximum size of the attribute. Allows +nil+ by
|
||||||
|
# default if not used with :minimum.
|
||||||
# * <tt>:is</tt> - The exact size of the attribute.
|
# * <tt>:is</tt> - The exact size of the attribute.
|
||||||
# * <tt>:within</tt> - A range specifying the minimum and maximum size of
|
# * <tt>:within</tt> - A range specifying the minimum and maximum size of
|
||||||
# the attribute.
|
# the attribute.
|
||||||
|
|
|
@ -375,4 +375,43 @@ class LengthValidationTest < ActiveModel::TestCase
|
||||||
t.author_name = "A very long author name that should still be valid." * 100
|
t.author_name = "A very long author name that should still be valid." * 100
|
||||||
assert t.valid?
|
assert t.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
|
||||||
|
Topic.validates_length_of :title, :maximum => 10, :allow_nil => false
|
||||||
|
t = Topic.new
|
||||||
|
assert t.invalid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed
|
||||||
|
Topic.validates_length_of :title, :maximum => 10, :allow_blank => false
|
||||||
|
t = Topic.new
|
||||||
|
assert t.invalid?
|
||||||
|
|
||||||
|
t.title = ""
|
||||||
|
assert t.invalid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil
|
||||||
|
Topic.validates_length_of :title, :minimum => 5, :maximum => 10
|
||||||
|
t = Topic.new
|
||||||
|
assert t.invalid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validates_length_of_using_minimum_0_should_not_allow_nil
|
||||||
|
Topic.validates_length_of :title, :minimum => 0
|
||||||
|
t = Topic.new
|
||||||
|
assert t.invalid?
|
||||||
|
|
||||||
|
t.title = ""
|
||||||
|
assert t.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validates_length_of_using_is_0_should_not_allow_nil
|
||||||
|
Topic.validates_length_of :title, :is => 0
|
||||||
|
t = Topic.new
|
||||||
|
assert t.invalid?
|
||||||
|
|
||||||
|
t.title = ""
|
||||||
|
assert t.valid?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue