From: Jean-Philippe Lang Date: Wed, 20 Jul 2011 16:29:34 +0000 (+0000) Subject: Validate custom field regexp (#8865). X-Git-Tag: 1.3.0~1645 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4f92276654fe7560e44cb8ae82df7f87fdd60eb1;p=redmine.git Validate custom field regexp (#8865). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6298 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 0e8151ee0..6697a192a 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -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 diff --git a/test/unit/custom_field_test.rb b/test/unit/custom_field_test.rb index 4d80f5f3a..d19e89095 100644 --- a/test/unit/custom_field_test.rb +++ b/test/unit/custom_field_test.rb @@ -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", ""]