Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#64348] Refine error message for invalid custom fields

Tobias Dillmannbd8e2cbfbd5b
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index 90ed5039aa9..2355566a121 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -95,7 +95,8 @@ module CustomField::CalculatedValue
surplus_cfs = formula_cfs - allowed_cfs
if surplus_cfs.any?
- errors.add(:formula, :invalid_custom_fields, surplus_cfs:)
+ custom_field_names = CustomField.where(id: surplus_cfs).pluck(:name)
+ errors.add(:formula, :invalid_custom_fields, custom_fields: custom_field_names.join(", "))
return
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9aa2b59506d..4a1f5d5defe 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1349,6 +1349,7 @@ en:
inclusion_nested: "is not set to one of the allowed values at path '%{path}'."
invalid: "is invalid."
invalid_characters: "contains invalid characters."
+ invalid_custom_fields: "contains custom fields that are not allowed: %{custom_fields}."
invalid_url: "is not a valid URL."
invalid_url_scheme: "is not a supported protocol (allowed: %{allowed_schemes})."
less_than_or_equal_to: "must be less than or equal to %{count}."
diff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rb
index e0978fa186d..eaa18579226 100644
--- a/spec/models/custom_field/calculated_value_spec.rb
+++ b/spec/models/custom_field/calculated_value_spec.rb
@@ -166,5 +166,27 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_proje
it_behaves_like "invalid formula", "is invalid."
end
+
+ context "with a formula that contains custom fields that are not visible to the user" do
+ let(:project_with_permission) { create(:project) }
+ let(:project_without_permission) { create(:project) }
+ let(:user) { create(:user, member_with_permissions: { project_with_permission => [:view_project_attributes] }) }
+
+ let!(:int) do
+ create(:project_custom_field, :integer, name: "int", default_value: 4, projects: [project_without_permission])
+ end
+ let!(:float) do
+ create(:project_custom_field, :float, name: "float", default_value: 5.5, projects: [project_without_permission])
+ end
+ let!(:other_calculated_value) do
+ create(:calculated_value_project_custom_field, formula: "2 + 2", projects: [project_with_permission])
+ end
+
+ let(:formula) { "1 + {{cf_#{int.id}}} + {{cf_#{float.id}}} + {{cf_#{other_calculated_value.id}}}" }
+
+ current_user { user }
+
+ it_behaves_like "invalid formula", "contains custom fields that are not allowed: int, float."
+ end
end
end