From 8667505e23b99d5ed025f9f26d87f41eb4c64aa0 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 1 Mar 2015 13:23:52 +0000 Subject: [PATCH] Don't validate custom fields that are not editable (#19193). git-svn-id: http://svn.redmine.org/redmine/trunk@14045 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/issue.rb | 11 +++++++++++ test/unit/issue_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/models/issue.rb b/app/models/issue.rb index 5a49b0d7b..dcc3930a5 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -640,6 +640,17 @@ class Issue < ActiveRecord::Base end end + # Overrides Redmine::Acts::Customizable::InstanceMethods#validate_custom_field_values + # so that custom values that are not editable are not validated (eg. a custom field that + # is marked as required should not trigger a validation error if the user is not allowed + # to edit this field). + def validate_custom_field_values + user = new_record? ? author : current_journal.try(:user) + if new_record? || custom_field_values_changed? + editable_custom_field_values(user).each(&:validate_value) + end + end + # Set the done_ratio using the status if that setting is set. This will keep the done_ratios # even if the user turns off the setting later def update_done_ratio_from_issue_status diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 33635e7a5..7f3e9484d 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -860,6 +860,29 @@ class IssueTest < ActiveSupport::TestCase assert issue.save end + def test_required_custom_field_that_is_not_visible_for_the_user_should_not_be_required + CustomField.delete_all + field = IssueCustomField.generate!(:is_required => true, :visible => false, :role_ids => [1], :trackers => Tracker.all, :is_for_all => true) + user = User.generate! + User.add_to_project(user, Project.find(1), Role.find(2)) + + issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, + :subject => 'Required fields', :author => user) + assert_save issue + end + + def test_required_custom_field_that_is_visible_for_the_user_should_be_required + CustomField.delete_all + field = IssueCustomField.generate!(:is_required => true, :visible => false, :role_ids => [1], :trackers => Tracker.all, :is_for_all => true) + user = User.generate! + User.add_to_project(user, Project.find(1), Role.find(1)) + + issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, + :subject => 'Required fields', :author => user) + assert !issue.save + assert_include "#{field.name} cannot be blank", issue.errors.full_messages + end + def test_required_attribute_names_for_multiple_roles_should_intersect_rules WorkflowPermission.delete_all WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, -- 2.39.5