Custom validator to check the presence of custom field name.

pull/1320/head
slawa 11 years ago
parent 371990e669
commit fd0f497c28
  1. 21
      app/models/custom_field.rb

@ -55,9 +55,10 @@ class CustomField < ActiveRecord::Base
alias_method_chain :translations_attributes=, :globalized
validates_presence_of :name, :field_format
validates_presence_of :field_format
validate :uniqueness_of_name_with_scope
def uniqueness_of_name_with_scope
taken_names = CustomField.where(:type => type)
taken_names = taken_names.where('id != ?', id) if id
@ -66,12 +67,13 @@ class CustomField < ActiveRecord::Base
errors.add(:name, :taken) if name.in?(taken_names)
end
validates_length_of :name, :maximum => 30
validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
validate :validate_presence_of_possible_values
validate :validate_default_value_in_translations
validate :validate_presence_of_name
def initialize(attributes = nil, options = {})
super
self.possible_values ||= []
@ -106,6 +108,21 @@ class CustomField < ActiveRecord::Base
self.is_required = required_field
end
def validate_presence_of_name
if self.translations.empty?
errors.add(:name, :blank) if self.name.nil?
else
fallback_name = self.translations.find{|el| el.name != nil}
self.translations.each do | translation |
if translation.name.nil? && fallback_name.nil?
errors.add(:name, :blank)
else
translation.name = fallback_name.name if translation.name.nil?
end
end
end
end
def possible_values_options(obj=nil)
case field_format
when 'user', 'version'

Loading…
Cancel
Save