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

[#55801] Form validation in add project dialog missing

Judith Roth9f99d2c65317
diff --git a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.html.erb b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.html.erb
index a7c014f7b73..6fe98a3cdc6 100644
--- a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.html.erb
+++ b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.html.erb
@@ -1,44 +1,18 @@
<%=
- component_wrapper do
- render(
- Primer::Alpha::Dialog.new(
- id: dialog_id,
- title:,
- test_selector: dialog_id,
- size: :large
- )
- ) do |dialog|
- dialog.with_show_button(scheme: :primary) do |button|
- button.with_leading_visual_icon(icon: 'op-include-projects')
- show_button_text
- end
+ render(
+ Primer::Alpha::Dialog.new(
+ id: Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingFormComponent::DIALOG_ID,
+ title:,
+ test_selector: Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingFormComponent::DIALOG_ID,
+ size: :large
+ )
+ ) do |dialog|
+ dialog.with_header(
+ show_divider: false,
+ visually_hide_title: false,
+ variant: :large
+ )
- dialog.with_header(
- show_divider: false,
- visually_hide_title: false,
- variant: :large
- )
-
- primer_form_with(
- class: "op-new-project-mapping-form",
- model: @project_mapping,
- url: link_admin_settings_project_custom_field_path(@project_custom_field),
- data: { turbo: true },
- method: :post
- ) do |form|
- concat(render(Primer::Alpha::Dialog::Body.new(
- id: dialog_body_id, test_selector: dialog_body_id, aria: { label: title },
- classes: "FormControl-horizontalGroup--sm-vertical FormControl-horizontalGroup--center-aligned",
- style: "min-height: 300px"
- )) do
- render(Projects::CustomFields::CustomFieldMappingForm.new(form, project_custom_field: @project_custom_field))
- end)
-
- concat(render(Primer::Alpha::Dialog::Footer.new(show_divider: false)) do
- concat(render(Primer::ButtonComponent.new(data: { 'close-dialog-id': dialog_id })) { cancel_button_text })
- concat(render(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) { submit_button_text })
- end)
- end
- end
+ render(Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingFormComponent.new(project_mapping: @project_mapping, project_custom_field: @project_custom_field))
end
%>
diff --git a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.rb b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.rb
index b6b8e71b9bb..c128e024aa9 100644
--- a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.rb
+++ b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_component.rb
@@ -32,9 +32,6 @@ module Settings
class NewProjectMappingComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
include OpTurbo::Streamable
- options dialog_id: "settings--new-project-custom-field-mapping-component",
- dialog_body_id: "settings--new-project-custom-field-mapping-body-component"
-
def initialize(project_mapping:, project_custom_field:, **)
@project_mapping = project_mapping
@project_custom_field = project_custom_field
@@ -44,24 +41,6 @@ module Settings
def render?
!@project_custom_field.required?
end
-
- private
-
- def title
- I18n.t("projects.settings.project_custom_fields.new_project_mapping_form.add_projects")
- end
-
- def show_button_text
- I18n.t("projects.settings.project_custom_fields.new_project_mapping_form.add_projects")
- end
-
- def cancel_button_text
- I18n.t("button_cancel")
- end
-
- def submit_button_text
- I18n.t("button_add")
- end
end
end
end
diff --git a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.html.erb b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.html.erb
new file mode 100644
index 00000000000..16bc25ac5e4
--- /dev/null
+++ b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.html.erb
@@ -0,0 +1,52 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 the OpenProject GmbH
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License version 3.
+
+OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+Copyright (C) 2006-2013 Jean-Philippe Lang
+Copyright (C) 2010-2013 the ChiliProject Team
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+See COPYRIGHT and LICENSE files for more details.
+
+++#%>
+
+<%=
+ component_wrapper do
+ primer_form_with(
+ class: "op-new-project-mapping-form",
+ model: @project_mapping,
+ url: link_admin_settings_project_custom_field_path(@project_custom_field),
+ data: { turbo: true },
+ method: :post
+ ) do |form|
+ concat(render(Primer::Alpha::Dialog::Body.new(
+ id: DIALOG_BODY_ID, test_selector: DIALOG_BODY_ID, aria: { label: title },
+ classes: "FormControl-horizontalGroup--sm-vertical FormControl-horizontalGroup--center-aligned",
+ style: "min-height: 300px"
+ )) do
+ render(Projects::CustomFields::CustomFieldMappingForm.new(form, project_mapping: @project_mapping))
+ end)
+ concat(render(Primer::Alpha::Dialog::Footer.new(show_divider: false)) do
+ concat(render(Primer::ButtonComponent.new(data: { 'close-dialog-id': DIALOG_ID })) { cancel_button_text })
+ concat(render(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) { submit_button_text })
+ end)
+ end
+ end
+%>
diff --git a/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.rb b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.rb
new file mode 100644
index 00000000000..bf6b51d6892
--- /dev/null
+++ b/app/components/settings/project_custom_fields/project_custom_field_mapping/new_project_mapping_form_component.rb
@@ -0,0 +1,60 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Settings
+ module ProjectCustomFields
+ module ProjectCustomFieldMapping
+ class NewProjectMappingFormComponent < ApplicationComponent
+ include OpTurbo::Streamable
+
+ DIALOG_ID = "settings--new-project-custom-field-mapping-component"
+ DIALOG_BODY_ID = "settings--new-project-custom-field-mapping-body-component"
+
+ def initialize(project_mapping:, project_custom_field: )
+ super
+ @project_mapping = project_mapping
+ @project_custom_field = project_custom_field
+ end
+
+ private
+
+ def title
+ I18n.t("projects.settings.project_custom_fields.new_project_mapping_form.add_projects")
+ end
+
+ def cancel_button_text
+ I18n.t("button_cancel")
+ end
+
+ def submit_button_text
+ I18n.t("button_add")
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rb
index 59d366d3d9a..625e51d5ce9 100644
--- a/app/controllers/admin/settings/project_custom_fields_controller.rb
+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb
@@ -30,6 +30,7 @@ module Admin::Settings
class ProjectCustomFieldsController < ::Admin::SettingsController
include CustomFields::SharedActions
include OpTurbo::ComponentStream
+ include OpTurbo::DialogStreamHelper
include ApplicationComponentStreams
include FlashMessagesOutputSafetyHelper
include Admin::Settings::ProjectCustomFields::ComponentStreams
@@ -39,7 +40,7 @@ module Admin::Settings
# rubocop:disable Rails/LexicallyScopedActionFilter
before_action :set_sections, only: %i[show index edit update move drop]
before_action :find_custom_field,
- only: %i(show edit project_mappings link unlink update destroy delete_option reorder_alphabetical move drop)
+ only: %i(show edit project_mappings new_link link unlink update destroy delete_option reorder_alphabetical move drop)
before_action :prepare_custom_option_position, only: %i(update create)
before_action :find_custom_option, only: :delete_option
before_action :project_custom_field_mappings_query, only: %i[project_mappings unlink]
@@ -69,8 +70,14 @@ module Admin::Settings
def edit; end
- def project_mappings
+ def project_mappings; end
+
+ def new_link
@project_mapping = ProjectCustomFieldProjectMapping.new(project_custom_field: @custom_field)
+ respond_with_dialog Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingComponent.new(
+ project_mapping: @project_mapping,
+ project_custom_field: @custom_field
+ )
end
def link
@@ -148,12 +155,6 @@ module Admin::Settings
private
def render_project_list(url_for_action: action_name)
- update_via_turbo_stream(
- component: Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingComponent.new(
- project_mapping: ProjectCustomFieldProjectMapping.new(project_custom_field: @custom_field),
- project_custom_field: @custom_field
- )
- )
update_via_turbo_stream(
component: Settings::ProjectCustomFields::ProjectCustomFieldMapping::TableComponent.new(
query: project_custom_field_mappings_query,
@@ -196,10 +197,23 @@ module Admin::Settings
end
def find_custom_field_projects_to_link
- @projects = Project.find(params.to_unsafe_h[:project_custom_field_project_mapping][:project_ids])
+ project_ids = params.to_unsafe_h[:project_custom_field_project_mapping][:project_ids]
+ if project_ids.present?
+ @projects = Project.find(project_ids)
+ else
+ project_mapping = ProjectCustomFieldProjectMapping.new(project_custom_field: @custom_field)
+ project_mapping.errors.add(:project_ids, :blank)
+ component = Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingFormComponent.new(
+ project_mapping: project_mapping,
+ project_custom_field: @custom_field
+ )
+ update_via_turbo_stream(component: component, status: :bad_request)
+ respond_with_turbo_streams
+ false
+ end
rescue ActiveRecord::RecordNotFound
update_flash_message_via_turbo_stream(
- message: t(:notice_file_not_found), full: true, dismiss_scheme: :hide, scheme: :danger
+ message: t(:notice_project_not_found), full: true, dismiss_scheme: :hide, scheme: :danger
)
render_project_list
diff --git a/app/forms/projects/custom_fields/custom_field_mapping_form.rb b/app/forms/projects/custom_fields/custom_field_mapping_form.rb
index f91ef2936a3..237fa6bdb7e 100644
--- a/app/forms/projects/custom_fields/custom_field_mapping_form.rb
+++ b/app/forms/projects/custom_fields/custom_field_mapping_form.rb
@@ -28,12 +28,15 @@
module Projects::CustomFields
class CustomFieldMappingForm < ApplicationForm
+ include OpPrimer::ComponentHelpers
+
form do |form|
form.group(layout: :horizontal) do |group|
group.project_autocompleter(
name: :id,
label: Project.model_name.human,
visually_hide_label: true,
+ validation_message: project_ids_error_message,
autocomplete_options: {
openDirectly: false,
focusDirectly: false,
@@ -53,16 +56,24 @@ module Projects::CustomFields
end
end
- def initialize(project_custom_field:)
+ def initialize(project_mapping:)
super()
- @project_custom_field = project_custom_field
+ @project_mapping = project_mapping
end
private
+ def project_ids_error_message
+ @project_mapping
+ .errors
+ .messages_for(:project_ids)
+ .to_sentence
+ .presence
+ end
+
def projects_with_custom_field_mapping
ProjectCustomFieldProjectMapping
- .where(project_custom_field: @project_custom_field)
+ .where(custom_field_id: @project_mapping.custom_field_id)
.pluck(:project_id)
.to_h { |id| [id, id] }
end
diff --git a/app/views/admin/settings/project_custom_fields/project_mappings.html.erb b/app/views/admin/settings/project_custom_fields/project_mappings.html.erb
index 7699c86ac2b..ed01914788c 100644
--- a/app/views/admin/settings/project_custom_fields/project_mappings.html.erb
+++ b/app/views/admin/settings/project_custom_fields/project_mappings.html.erb
@@ -37,10 +37,15 @@ See COPYRIGHT and LICENSE files for more details.
<%=
render(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_component do
- render(Settings::ProjectCustomFields::ProjectCustomFieldMapping::NewProjectMappingComponent.new(
- project_mapping: @project_mapping,
- project_custom_field: @custom_field
- ))
+ render(Primer::Beta::Button.new(
+ scheme: :primary,
+ tag: :a,
+ href: new_link_admin_settings_project_custom_field_path(@custom_field),
+ data: { controller: "async-dialog" }
+ )) do |button|
+ button.with_leading_visual_icon(icon: 'op-include-projects')
+ I18n.t("projects.settings.project_custom_fields.new_project_mapping_form.add_projects")
+ end
end
end unless @custom_field.required?
%>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 7bd8db3d972..80e31ee4641 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -995,6 +995,10 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
enabled_modules:
dependency_missing: "The module '%{dependency}' needs to be enabled as well since the module '%{module}' depends on it."
format: "%{message}"
+ project_custom_field_project_mapping:
+ attributes:
+ project_ids:
+ blank: "Please select a project."
query:
attributes:
project:
@@ -2732,6 +2736,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
notice_principals_found_multiple: "There are %{number} results found. \n Tab to focus the first result."
notice_principals_found_single: "There is one result. \n Tab to focus it."
notice_project_not_deleted: "The project wasn't deleted."
+ notice_project_not_found: "Project not found."
notice_successful_connection: "Successful connection."
notice_successful_create: "Successful creation."
notice_successful_delete: "Successful deletion."
diff --git a/config/routes.rb b/config/routes.rb
index 8c273c0506c..5f5748fbc3d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -466,6 +466,7 @@ Rails.application.routes.draw do
put :drop
get :project_mappings
+ get :new_link
post :link
delete :unlink
end
diff --git a/spec/features/admin/project_custom_fields/project_mappings_spec.rb b/spec/features/admin/project_custom_fields/project_mappings_spec.rb
index 1f367fb5fcf..b5571a328f1 100644
--- a/spec/features/admin/project_custom_fields/project_mappings_spec.rb
+++ b/spec/features/admin/project_custom_fields/project_mappings_spec.rb
@@ -86,6 +86,18 @@ RSpec.describe "Project Custom Field Mappings", :js do
end
end
+ it "shows an error in the dialog when no project is selected before adding" do
+ create(:project)
+ expect(page).not_to have_css("dialog")
+ click_on "Add projects"
+
+ page.within("dialog") do
+ click_on "Add"
+
+ expect(page).to have_text("Please select a project.")
+ end
+ end
+
it "allows linking a project to a custom field" do
project = create(:project)
subproject = create(:project, parent: project)