]> source.dussan.org Git - redmine.git/commitdiff
Prevent unexpected nil in custom value validation.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 24 Jan 2008 18:15:38 +0000 (18:15 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 24 Jan 2008 18:15:38 +0000 (18:15 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1101 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/custom_value.rb
test/unit/custom_value_test.rb

index 94b797bcca315dcb174fa3170aec7dc669de34a3..98ce6b168617126cd320cc61ab52bf3ed2ce5d53 100644 (file)
@@ -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
index 4d488bae222ff05d1c83212b08ae427f52841f28..11578ae6b95978253bc2251404868cfe165ee39f 100644 (file)
@@ -20,7 +20,79 @@ require File.dirname(__FILE__) + '/../test_helper'
 class CustomValueTest < Test::Unit::TestCase
   fixtures :custom_fields
 
-  def test_float_field
+  def test_string_field_validation_with_blank_value
+    f = CustomField.new(:field_format => 'string')
+    v = CustomValue.new(:custom_field => f)
+    
+    v.value = nil
+    assert v.valid?
+    v.value = ''
+    assert v.valid?
+
+    f.is_required = true
+    v.value = nil
+    assert !v.valid?
+    v.value = ''
+    assert !v.valid?
+  end
+  
+  def test_string_field_validation_with_min_and_max_lengths
+    f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
+    v = CustomValue.new(:custom_field => f, :value => '')
+    assert v.valid?
+    v.value = 'a'
+    assert !v.valid?
+    v.value = 'a' * 2
+    assert v.valid?
+    v.value = 'a' * 6
+    assert !v.valid?
+  end
+  
+  def test_string_field_validation_with_regexp
+    f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
+    v = CustomValue.new(:custom_field => f, :value => '')
+    assert v.valid?
+    v.value = 'abc'
+    assert !v.valid?
+    v.value = 'ABC'
+    assert v.valid?
+  end
+
+  def test_date_field_validation
+    f = CustomField.new(:field_format => 'date')
+    v = CustomValue.new(:custom_field => f, :value => '')
+    assert v.valid?
+    v.value = 'abc'
+    assert !v.valid?
+    v.value = '1975-07-14'
+    assert v.valid?
+  end
+
+  def test_list_field_validation
+    f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
+    v = CustomValue.new(:custom_field => f, :value => '')
+    assert v.valid?
+    v.value = 'abc'
+    assert !v.valid?
+    v.value = 'value2'
+    assert v.valid?
+  end
+
+  def test_int_field_validation
+    f = CustomField.new(:field_format => 'int')
+    v = CustomValue.new(:custom_field => f, :value => '')
+    assert v.valid?
+    v.value = 'abc'
+    assert !v.valid?
+    v.value = '123'
+    assert v.valid?
+    v.value = '+123'
+    assert v.valid?
+    v.value = '-123'
+    assert v.valid?
+  end
+  
+  def test_float_field_validation
     v = CustomValue.new(:customized => User.find(:first), :custom_field => UserCustomField.find_by_name('Money'))
     v.value = '11.2'
     assert v.save