summaryrefslogtreecommitdiffstats
path: root/app/models/custom_value.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2008-01-24 18:15:38 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2008-01-24 18:15:38 +0000
commite13f49d91951a82d9989c2a0f0c4981832f2664f (patch)
treed302dde43b241690a16e03cd0d39dad71145fc53 /app/models/custom_value.rb
parent7a1e928b8db0cc0a584011973042522cd7b38d4d (diff)
downloadredmine-e13f49d91951a82d9989c2a0f0c4981832f2664f.tar.gz
redmine-e13f49d91951a82d9989c2a0f0c4981832f2664f.zip
Prevent unexpected nil in custom value validation.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1101 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/custom_value.rb')
-rw-r--r--app/models/custom_value.rb31
1 files changed, 18 insertions, 13 deletions
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index 94b797bcc..98ce6b168 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -27,19 +27,24 @@ class CustomValue < ActiveRecord::Base
protected
def validate
- errors.add(:value, :activerecord_error_blank) and return if custom_field.is_required? and value.blank?
- errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
- errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
- errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
- case custom_field.field_format
- when 'int'
- errors.add(:value, :activerecord_error_not_a_number) unless value.blank? || value =~ /^[+-]?\d+$/
- when 'float'
- begin; !value.blank? && Kernel.Float(value); rescue; errors.add(:value, :activerecord_error_invalid) end
- when 'date'
- errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ or value.blank?
- when 'list'
- errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include?(value) or value.blank?
+ if value.blank?
+ errors.add(:value, :activerecord_error_blank) if custom_field.is_required? and value.blank?
+ else
+ errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
+ errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length
+ errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
+
+ # Format specific validations
+ case custom_field.field_format
+ when 'int'
+ errors.add(:value, :activerecord_error_not_a_number) unless value =~ /^[+-]?\d+$/
+ when 'float'
+ begin; Kernel.Float(value); rescue; errors.add(:value, :activerecord_error_invalid) end
+ when 'date'
+ errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/
+ when 'list'
+ errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include?(value)
+ end
end
end
end