Validate custom field regexp (#8865).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6298 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-07-20 16:29:34 +00:00
parent 0a05da4f33
commit 4f92276654
2 changed files with 19 additions and 2 deletions

View File

@ -42,6 +42,14 @@ class CustomField < ActiveRecord::Base
errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
end
if regexp.present?
begin
Regexp.new(regexp)
rescue
errors.add(:regexp, :invalid)
end
end
# validate default value
v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
v.custom_field.is_required = false

View File

@ -1,5 +1,5 @@
# redMine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang
# Redmine - project management software
# Copyright (C) 2006-2011 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
@ -25,6 +25,15 @@ class CustomFieldTest < ActiveSupport::TestCase
assert field.save
end
def test_regexp_validation
field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
assert !field.save
assert_equal I18n.t('activerecord.errors.messages.invalid'), field.errors.on(:regexp)
field.regexp = '[a-z0-9]'
assert field.save
end
def test_possible_values_should_accept_an_array
field = CustomField.new
field.possible_values = ["One value", ""]