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

show validation error on the WP field when the project is not set

Klaus Zanders5acb7f28a006
diff --git a/modules/costs/app/components/time_entries/work_package_form.rb b/modules/costs/app/components/time_entries/work_package_form.rb
index b351089af42..142248f7cb9 100644
--- a/modules/costs/app/components/time_entries/work_package_form.rb
+++ b/modules/costs/app/components/time_entries/work_package_form.rb
@@ -41,7 +41,8 @@ module TimeEntries
if show_work_package_field?
f.work_package_autocompleter name: :work_package_id,
label: TimeEntry.human_attribute_name(:work_package),
- required: true,
+ required: work_package_required?,
+ validation_message: work_package_validation_error,
autocomplete_options: {
defaultData: false,
component: "opce-time-entries-work-package-autocompleter",
@@ -72,6 +73,20 @@ module TimeEntries
end
end
+ # When logging time from a project page or the work package page, the project id field is set in the background.
+ # When logging from the my page the project is only settable via the work package so in this case we need to make
+ # the WP field mandatory and get the error message from the project_id field and move it to the work_package_id field.
+ #
+ # We're still discussing if we make the work package mandatory, then this will become obsolete and
+ # probably be removed.
+ def work_package_required?
+ model.project.blank?
+ end
+
+ def work_package_validation_error
+ model.errors[:project_id]&.first
+ end
+
def work_package_completer_filters
filters = []
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index 2e3cf9b7788..d00f4480ace 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -48,6 +48,7 @@ class TimeEntry < ApplicationRecord
validates :user_id, :project_id, :spent_on,
presence: true
+
validates :hours,
presence: true,
if: -> { !ongoing? }