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

[33766] Show errors when CF is invalid in project form

Oliver Günthera91ab2e686c2
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index f5e34c5300f..f2da3ef0d53 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -103,10 +103,14 @@ class ProjectsController < ApplicationController
model: @altered_project)
.call(permitted_params.project)
- @errors = service_call.errors
- flash[:notice] = t(:notice_successful_update) if service_call.success?
- redirect_to settings_generic_project_path(@altered_project)
+ if service_call.success?
+ flash[:notice] = t(:notice_successful_update)
+ redirect_to settings_generic_project_path(@altered_project)
+ else
+ @errors = service_call.errors
+ render template: 'project_settings/generic'
+ end
end
def update_identifier
diff --git a/app/views/projects/form/attributes/_identifier.html.erb b/app/views/projects/form/attributes/_identifier.html.erb
index 5a0e4793009..772c912c209 100644
--- a/app/views/projects/form/attributes/_identifier.html.erb
+++ b/app/views/projects/form/attributes/_identifier.html.erb
@@ -34,18 +34,18 @@ See docs/COPYRIGHT.rdoc for more details.
identifier_editable = project.new_record?
%>
+<%= nonced_javascript_tag do %>
+ window.projectIdentifierMaxLength = <%= id_max_length %>;
+ window.projectIdentifierDefault = '<%= id %>';
+ window.projectIdentifierLocked = false;
+<% end %>
+
<div class="form--field <%= identifier_editable ? '-required' : '' %>">
<% if identifier_editable %>
<%= form.text_field :identifier,
help_text: { attribute: :identifier, 'attribute-scope': :Project },
container_class: '-middle'%>
- <%= nonced_javascript_tag do %>
- window.projectIdentifierMaxLength = <%= id_max_length %>;
- window.projectIdentifierDefault = '<%= id %>';
- window.projectIdentifierLocked = false;
- <% end %>
-
<div class="form--field-instructions">
<%= l(:text_length_between, min: 1,
max: Project::IDENTIFIER_MAX_LENGTH) %>
diff --git a/frontend/src/app/modules/augmenting/dynamic-scripts/project_form_listener.js b/frontend/src/app/modules/augmenting/dynamic-scripts/project_form_listener.js
index 8a62f569976..19b8d89c82c 100644
--- a/frontend/src/app/modules/augmenting/dynamic-scripts/project_form_listener.js
+++ b/frontend/src/app/modules/augmenting/dynamic-scripts/project_form_listener.js
@@ -82,12 +82,12 @@
identifier = identifier.replace(/[^a-z0-9]+/gi, '-'); // remaining non-alphanumeric => hyphen
identifier = identifier.replace(/^[-\d]*|-*$/g, ''); // remove hyphens and numbers at beginning and hyphens at end
identifier = identifier.toLowerCase(); // to lower
- identifier = identifier.substr(0,projectIdentifierMaxLength); // max characters
+ identifier = identifier.substr(0,window.projectIdentifierMaxLength); // max characters
return identifier;
}
function updateIdentifierFromName() {
- if(!projectIdentifierLocked) {
+ if(!window.projectIdentifierLocked) {
jQuery('#project_identifier').val(generateProjectIdentifier());
}
}
@@ -99,7 +99,7 @@
function observeProjectIdentifier() {
jQuery('#project_identifier').keyup(function() {
- projectIdentifierLocked = this.value !== '' && this.value != generateProjectIdentifier();
+ window.projectIdentifierLocked = this.value !== '' && this.value != generateProjectIdentifier();
});
}
diff --git a/spec/features/projects/projects_spec.rb b/spec/features/projects/projects_spec.rb
index 169b2dd8ef2..6cbc1e5cd10 100644
--- a/spec/features/projects/projects_spec.rb
+++ b/spec/features/projects/projects_spec.rb
@@ -213,5 +213,28 @@ describe 'Projects', type: :feature do
expect(page).to have_content 'Required Foo'
expect(page).to have_content 'Optional Foo'
end
+
+ context 'with a restricted custom field' do
+ let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
+ let!(:required_custom_field) do
+ FactoryBot.create(:string_project_custom_field,
+ name: 'Foo',
+ type: ProjectCustomField,
+ min_length: 1,
+ max_length: 2,
+ is_for_all: true)
+ end
+
+ it 'shows the errors of that field when saving (Regression #33766)' do
+ visit settings_generic_project_path(project.id)
+
+ expect(page).to have_content 'Foo'
+ # Enter something too long
+ fill_in 'Foo', with: '1234'
+
+ click_on 'Save'
+ expect(page).to have_selector('#errorExplanation', text: 'Foo is too long (maximum is 2 characters)')
+ end
+ end
end
end