Defect #9394

Custom date field only validating on regex and not a valid date

Added by Chris Malek about 6 years ago. Updated almost 6 years ago.

Status:ClosedStart date:2011-10-09
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Custom fields
Target version:1.2.3
Resolution:Fixed Affected version:1.2.2

Description

If you create a custom date field it looks like the validations are not correctly happening based on a valid date. It is only validating on this regex /^\d{4}-\d{2}-\d{2}$

So 2011-10-33 is a valid value but October does not have 33 days.

The offending code is in Redmine/apps/models/custom_value.rb

In the Validate method

I added the following to the end of the date validation && begin; value.to_date; rescue; false end

I only use git so I can't submit this as an svn patch.

Here is my modified code with the only changing being in the "when 'date'"

def validate
    if value.blank?
      errors.add(:value, :blank) if custom_field.is_required? and value.blank?    
    else
      errors.add(:value, :invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
      errors.add(:value, :too_short, :count => custom_field.min_length) if custom_field.min_length > 0 and value.length < custom_field.min_length
      errors.add(:value, :too_long, :count => custom_field.max_length) 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, :not_a_number) unless value =~ /^[+-]?\d+$/    
      when 'float'
        begin; Kernel.Float(value); rescue; errors.add(:value, :invalid) end
      when 'date'
        errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
      when 'list'
        errors.add(:value, :inclusion) unless custom_field.possible_values.include?(value)
      end
    end
  end

My Environment Info

Redmine 1.0.4.stable (MySQL)

Running using the latest Bitnami windows installer


Related issues

Related to Redmine - Defect #12359: Version date field regex validation accepts invalid date Closed

Associated revisions

Revision 7779
Added by Jean-Philippe Lang about 6 years ago

Custom date field accepts invalid dates (#9394).

History

#1 Updated by Jean-Philippe Lang about 6 years ago

  • Status changed from New to Resolved
  • Target version set to 1.2.3
  • Affected version (unused) set to 1.2.2
  • Resolution set to Fixed
  • Affected version set to 1.2.2

Fixed with test in r7779, thanks.

#2 Updated by Jean-Philippe Lang almost 6 years ago

  • Status changed from Resolved to Closed

Merged in r7998.

Also available in: Atom PDF