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 | |
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')
-rw-r--r-- | app/helpers/custom_fields_helper.rb | 5 | ||||
-rw-r--r-- | app/models/custom_field.rb | 54 | ||||
-rw-r--r-- | app/models/custom_field_value.rb | 50 | ||||
-rw-r--r-- | app/models/custom_value.rb | 27 |
4 files changed, 100 insertions, 36 deletions
diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 68e7d987d..7b1927c0a 100644 --- a/app/helpers/custom_fields_helper.rb +++ b/app/helpers/custom_fields_helper.rb @@ -1,7 +1,7 @@ # encoding: utf-8 # # 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 @@ -61,8 +61,7 @@ module CustomFieldsHelper def custom_field_label_tag(name, custom_value) content_tag "label", h(custom_value.custom_field.name) + (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""), - :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}", - :class => (custom_value.errors.empty? ? nil : "error" ) + :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}" end # Return custom field tag with its label tag 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 diff --git a/app/models/custom_field_value.rb b/app/models/custom_field_value.rb new file mode 100644 index 000000000..ec243af1d --- /dev/null +++ b/app/models/custom_field_value.rb @@ -0,0 +1,50 @@ +# Redmine - project management software +# 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 +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class CustomFieldValue + attr_accessor :custom_field, :customized, :value + + def custom_field_id + custom_field.id + end + + def true? + self.value == '1' + end + + def editable? + custom_field.editable? + end + + def visible? + custom_field.visible? + end + + def required? + custom_field.is_required? + end + + def to_s + value.to_s + end + + def validate_value + custom_field.validate_field_value(value).each do |message| + customized.errors.add(:base, custom_field.name + ' ' + message) + end + end +end diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb index 6cb4853f9..89d3709cc 100644 --- a/app/models/custom_value.rb +++ b/app/models/custom_value.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 @@ -19,8 +19,6 @@ class CustomValue < ActiveRecord::Base belongs_to :custom_field belongs_to :customized, :polymorphic => true - validate :validate_custom_value - def initialize(attributes=nil, *args) super if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?)) @@ -48,27 +46,4 @@ class CustomValue < ActiveRecord::Base def to_s value.to_s end - -protected - def validate_custom_value - 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 end |