]> source.dussan.org Git - redmine.git/commitdiff
Don't validate custom fields that are not editable (#19193).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 1 Mar 2015 13:23:52 +0000 (13:23 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 1 Mar 2015 13:23:52 +0000 (13:23 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14045 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
test/unit/issue_test.rb

index 5a49b0d7bae169b214fe92353dec6f9ec5d14fd6..dcc3930a586939558dbaa8486f45c611305868eb 100644 (file)
@@ -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
index 33635e7a5795a81d20b6840a5c282e93edab8ef4..7f3e9484d61a4dd99e86aa1c4a6fabb0dc0da369 100644 (file)
@@ -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,