Adds a custom validator for dates (#12736).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11124 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
6ed7e091df
commit
3e14c3017c
|
@ -68,6 +68,8 @@ class Issue < ActiveRecord::Base
|
||||||
validates_length_of :subject, :maximum => 255
|
validates_length_of :subject, :maximum => 255
|
||||||
validates_inclusion_of :done_ratio, :in => 0..100
|
validates_inclusion_of :done_ratio, :in => 0..100
|
||||||
validates_numericality_of :estimated_hours, :allow_nil => true
|
validates_numericality_of :estimated_hours, :allow_nil => true
|
||||||
|
validates :start_date, :date => true
|
||||||
|
validates :due_date, :date => true
|
||||||
validate :validate_issue, :validate_required_fields
|
validate :validate_issue, :validate_required_fields
|
||||||
|
|
||||||
scope :visible, lambda {|*args|
|
scope :visible, lambda {|*args|
|
||||||
|
@ -532,14 +534,6 @@ class Issue < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_issue
|
def validate_issue
|
||||||
if due_date.nil? && @attributes['due_date'].present?
|
|
||||||
errors.add :due_date, :not_a_date
|
|
||||||
end
|
|
||||||
|
|
||||||
if start_date.nil? && @attributes['start_date'].present?
|
|
||||||
errors.add :start_date, :not_a_date
|
|
||||||
end
|
|
||||||
|
|
||||||
if due_date && start_date && due_date < start_date
|
if due_date && start_date && due_date < start_date
|
||||||
errors.add :due_date, :greater_than_start_date
|
errors.add :due_date, :greater_than_start_date
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,10 +30,9 @@ class Version < ActiveRecord::Base
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
validates_uniqueness_of :name, :scope => [:project_id]
|
validates_uniqueness_of :name, :scope => [:project_id]
|
||||||
validates_length_of :name, :maximum => 60
|
validates_length_of :name, :maximum => 60
|
||||||
validates_format_of :effective_date, :with => /\A\d{4}-\d{2}-\d{2}\z/, :message => :not_a_date, :allow_nil => true
|
validates :effective_date, :date => true
|
||||||
validates_inclusion_of :status, :in => VERSION_STATUSES
|
validates_inclusion_of :status, :in => VERSION_STATUSES
|
||||||
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
|
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
|
||||||
validate :validate_version
|
|
||||||
|
|
||||||
scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
|
scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
|
||||||
scope :open, lambda { where(:status => 'open') }
|
scope :open, lambda { where(:status => 'open') }
|
||||||
|
@ -287,10 +286,4 @@ class Version < ActiveRecord::Base
|
||||||
progress
|
progress
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_version
|
|
||||||
if effective_date.nil? && @attributes['effective_date'].present?
|
|
||||||
errors.add :effective_date, :not_a_date
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,3 +38,14 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DateValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
before_type_cast = record.attributes_before_type_cast[attribute.to_s]
|
||||||
|
if before_type_cast.is_a?(String) && before_type_cast.present?
|
||||||
|
unless before_type_cast =~ /\A\d{4}-\d{2}-\d{2}\z/ && value
|
||||||
|
record.errors.add attribute, :not_a_date
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -37,6 +37,8 @@ class VersionTest < ActiveSupport::TestCase
|
||||||
assert !v.valid?
|
assert !v.valid?
|
||||||
v.effective_date = '2012-31-11'
|
v.effective_date = '2012-31-11'
|
||||||
assert !v.valid?
|
assert !v.valid?
|
||||||
|
v.effective_date = '-2012-31-11'
|
||||||
|
assert !v.valid?
|
||||||
v.effective_date = 'ABC'
|
v.effective_date = 'ABC'
|
||||||
assert !v.valid?
|
assert !v.valid?
|
||||||
assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
|
assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
|
||||||
|
|
Loading…
Reference in New Issue