diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-28 11:16:58 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-28 11:16:58 +0000 |
commit | 83e7ee6729cd0207219719556b3e2aed0a33f360 (patch) | |
tree | 8fa089641bcaa6baec5aee080431e4e38c731a51 /app/models/custom_field.rb | |
parent | d4d27bd2d8b57e5d2597b75fbb7b963b6c3e37f7 (diff) | |
download | redmine-83e7ee6729cd0207219719556b3e2aed0a33f360.tar.gz redmine-83e7ee6729cd0207219719556b3e2aed0a33f360.zip |
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8717 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/custom_field.rb')
-rw-r--r-- | app/models/custom_field.rb | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 87285ed66..0dd70de1d 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -1,5 +1,5 @@ # Redmine - project management software -# Copyright (C) 2006-2011 Jean-Philippe Lang +# Copyright (C) 2006-2012 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -27,7 +27,7 @@ class CustomField < ActiveRecord::Base validates_length_of :name, :maximum => 30 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats - validate :validate_values + validate :validate_custom_field before_validation :set_searchable def initialize(attributes=nil, *args) @@ -41,7 +41,7 @@ class CustomField < ActiveRecord::Base true end - def validate_values + def validate_custom_field if self.field_format == "list" errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array @@ -55,10 +55,9 @@ class CustomField < ActiveRecord::Base end end - # validate default value - v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) - v.custom_field.is_required = false - errors.add(:default_value, :invalid) unless v.valid? + unless valid_field_value?(default_value) + errors.add(:default_value, :invalid) + end end def possible_values_options(obj=nil) @@ -161,4 +160,45 @@ class CustomField < ActiveRecord::Base def type_name nil end + + # Returns the error message for the given value + # or an empty array if value is a valid value for the custom field + def validate_field_value(value) + errs = [] + if is_required? && value.blank? + errs << ::I18n.t('activerecord.errors.messages.blank') + end + errs += validate_field_value_format(value) + errs + end + + # Returns true if value is a valid value for the custom field + def valid_field_value?(value) + validate_field_value(value).empty? + end + + protected + + # Returns the error message for the given value regarding its format + def validate_field_value_format(value) + errs = [] + if value.present? + errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) + errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length + errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length + + # Format specific validations + case field_format + when 'int' + errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ + when 'float' + begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end + when 'date' + errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end + when 'list' + errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) + end + end + errs + end end |