summaryrefslogtreecommitdiffstats
path: root/redmine/app/models/custom_value.rb
diff options
context:
space:
mode:
Diffstat (limited to 'redmine/app/models/custom_value.rb')
-rw-r--r--redmine/app/models/custom_value.rb42
1 files changed, 22 insertions, 20 deletions
diff --git a/redmine/app/models/custom_value.rb b/redmine/app/models/custom_value.rb
index faaa8ff82..ac72e5aba 100644
--- a/redmine/app/models/custom_value.rb
+++ b/redmine/app/models/custom_value.rb
@@ -16,26 +16,28 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class CustomValue < ActiveRecord::Base
- belongs_to :issue
- belongs_to :custom_field
-
+ belongs_to :custom_field
+ belongs_to :customized, :polymorphic => true
+
protected
def validate
- errors.add(custom_field.name, "can't be blank") if custom_field.is_required? and value.empty?
- errors.add(custom_field.name, "is not valid") unless custom_field.regexp.empty? or value =~ Regexp.new(custom_field.regexp)
-
- case custom_field.typ
- when 0
- errors.add(custom_field.name, "must be an integer") unless value =~ /^[0-9]*$/
- when 1
- errors.add(custom_field.name, "is too short") if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
- errors.add(custom_field.name, "is too long") if custom_field.max_length > 0 and value.length > custom_field.max_length
- when 2
- errors.add(custom_field.name, "must be a valid date") unless value =~ /^(\d+)\/(\d+)\/(\d+)$/ or value.empty?
- when 3
-
- when 4
- errors.add(custom_field.name, "is not a valid value") unless custom_field.possible_values.split('|').include? value or value.empty?
- end
+ # errors are added to customized object unless it's nil
+ object = customized || self
+
+ object.errors.add(custom_field.name, :activerecord_error_blank) if custom_field.is_required? and value.empty?
+ object.errors.add(custom_field.name, :activerecord_error_invalid) unless custom_field.regexp.empty? or value =~ Regexp.new(custom_field.regexp)
+
+ object.errors.add(custom_field.name, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
+ object.errors.add(custom_field.name, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
+
+ case custom_field.field_format
+ when "int"
+ object.errors.add(custom_field.name, :activerecord_error_not_a_number) unless value =~ /^[0-9]*$/
+ when "date"
+ object.errors.add(custom_field.name, :activerecord_error_invalid) unless value =~ /^(\d+)\/(\d+)\/(\d+)$/ or value.empty?
+ when "list"
+ object.errors.add(custom_field.name, :activerecord_error_inclusion) unless custom_field.possible_values.split('|').include? value or value.empty?
+ end
end
-end
+end
+