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

Add form preview documentation and create a generic form preview stimulus controller

Dombi Attila9431f9d6f7c9
diff --git a/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts
index 1cc2421b730..ec075b748f5 100644
--- a/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts
@@ -31,22 +31,13 @@
import { Controller } from '@hotwired/stimulus';
export default class ProjectLifeCyclesFormController extends Controller {
- static targets = ['form'];
-
- declare readonly formTarget:HTMLFormElement;
-
handleChange(event:Event) {
const target = event.target as HTMLElement;
- const previewUrl = this.formTarget.dataset.previewUrl;
-
- if (!previewUrl || this.datePickerVisible(target)) {
+ if (this.datePickerVisible(target)) {
return; // flatpickr is still open, do not submit yet.
}
- const form = this.formTarget;
- form.action = previewUrl;
-
- form.requestSubmit();
+ this.dispatch('triggerFormPreview');
}
datePickerVisible(element:HTMLElement) {
diff --git a/frontend/src/stimulus/controllers/form-preview.controller.ts b/frontend/src/stimulus/controllers/form-preview.controller.ts
new file mode 100644
index 00000000000..ce6560fc894
--- /dev/null
+++ b/frontend/src/stimulus/controllers/form-preview.controller.ts
@@ -0,0 +1,56 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 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.
+ * ++
+ */
+
+import { ApplicationController } from 'stimulus-use';
+
+export default class FormPreviewController extends ApplicationController {
+ static values = { url: String };
+
+ declare readonly formTarget:HTMLFormElement;
+ declare urlValue:string;
+
+ connect() {
+ // Ensure this.element is a form element
+ if (!(this.element instanceof HTMLFormElement)) {
+ throw new Error('The controller must be bound to a <form> element');
+ }
+ }
+
+ async submit():Promise<void> {
+ if (!this.urlValue) {
+ return;
+ }
+
+ const form = this.element as HTMLFormElement;
+ form.action = this.urlValue;
+
+ form.requestSubmit();
+ }
+}
diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.ts
index b1b052b5e8c..022ec08d973 100644
--- a/frontend/src/stimulus/setup.ts
+++ b/frontend/src/stimulus/setup.ts
@@ -5,6 +5,7 @@ import MainMenuController from './controllers/dynamic/menus/main.controller';
import OpDisableWhenCheckedController from './controllers/disable-when-checked.controller';
import PrintController from './controllers/print.controller';
import RefreshOnFormChangesController from './controllers/refresh-on-form-changes.controller';
+import FormPreviewController from './controllers/form-preview.controller';
import AsyncDialogController from './controllers/async-dialog.controller';
import PollForChangesController from './controllers/poll-for-changes.controller';
import TableHighlightingController from './controllers/table-highlighting.controller';
@@ -37,6 +38,7 @@ instance.register('password-confirmation-dialog', PasswordConfirmationDialogCont
instance.register('poll-for-changes', PollForChangesController);
instance.register('print', PrintController);
instance.register('refresh-on-form-changes', RefreshOnFormChangesController);
+instance.register('form-preview', FormPreviewController);
instance.register('show-when-checked', OpShowWhenCheckedController);
instance.register('show-when-value-selected', OpShowWhenValueSelectedController);
instance.register('table-highlighting', TableHighlightingController);
diff --git a/lookbook/docs/patterns/02-forms.md.erb b/lookbook/docs/patterns/02-forms.md.erb
index b757110c941..3e05c957bc8 100644
--- a/lookbook/docs/patterns/02-forms.md.erb
+++ b/lookbook/docs/patterns/02-forms.md.erb
@@ -166,6 +166,130 @@ Important data inputs:
- If you want the target to be visibly blocked, not hidden, then use `data: { set_visibility: "true" }`. By default, the field will be hidden and removed from DOM computation.
+### Advanced Interactivity by previewing forms before submission
+
+In order to implement complex form interactions other than hiding or showing fields, we can use the form preview pattern. A few examples of such interactions include displaying instant form validation errors on a changed field, or on its related fields. Another example is updating the caption of an input field based on its value. Handling each element update separately in these scenarios would be too cumbersome. A much better approach is to re-render the whole form when a field changes, preferably using a turbo streams morph response.
+
+In the example below we can see how the preview mechanism can be applied to forms:
+
+<%= embed Patterns::FormsPreview, :form_preview %>
+
+#### How it works:
+
+The form preview mechanism can be applied to any form by binding the `form-preview` stimulus controller to it and then watch the input fields for changes:
+
+ ```ruby
+ primer_form_with(
+ url: "/foo",
+ method: :get,
+ data: {
+ "controller": "form-preview",
+ "form-preview-url-value": preview_path
+ }
+ ) do |f|
+ f.text_field(name: :answer, data: { action: "change->form-preview#submit" })
+ end
+ ```
+- `"controller": "form-preview"` will activate the stimulus controller that processes the form refresh.
+- `"form-preview-url-value"` defines the path to be used for submitting the form preview.
+- Setting the `data: { action: "change->form-preview#submit" }` on an individual input field will trigger the form preview action when the field is changed.
+
+#### Customizing the triggering mechanism:
+
+In some cases we might want to further customize the triggering behaviour, for example when using a date range picker input field. For date range pickers we want to trigger the form preview only when the both the start and end dates are chosen and the datepicker is closed. The solution is to create a form specific controller that will decide if the `form-preview` controller should be called.
+
+1. The form specific controller handles the input changes on the form. It also dispatches the arbitrarily chosen `triggerFormPreview` event when the date range picker is not visible anymore.
+
+ ```typescript
+ export default class CustomFormPreviewFormController extends Controller {
+ handleChange(event:Event) {
+ const target = event.target as HTMLElement;
+ if (this.datePickerVisible(target)) {
+ return; // The datepicker is still open, do not submit yet.
+ }
+
+ this.dispatch('triggerFormPreview');
+ }
+ }
+ ```
+2. In order to chain the `custom-form-preview` and `form-preview` controller events, the following definition needs to be added on the form:
+
+ ```ruby
+ primer_form_with(
+ url: "/foo",
+ method: :get,
+ data: {
+ "controller": "custom-form-preview form-preview",
+ "action": "custom-form-preview:triggerFormPreview->form-preview#submit",
+ "form-preview-url-value": preview_path
+ }
+ ) do |f|
+ f.text_field(name: :answer, data: { action: "change->custom-form-preview#handleChange" })
+ end
+ ```
+ - The `"action": "custom-form-preview:triggerFormPreview->form-preview#submit"` will route the `custom-form-preview#handleChange` action to the `form-preview#submit` action via the dispatched `triggerFormPreview` event.
+ - The `data: { action: "change->custom-form-preview#handleChange" }` watches the input changes and calls the `custom-form-preview#handleChange`.
+
+**Important note:** Javascript rendered elements inside the form such as the datepicker above, could be broken after the form update. This happens, because the datepicker input get replaced without re-initializing the datepicker library. To fix the issue, we can either avoid updating the datepicker input elements using "data-turbo-permanent", or we can programatically re-initialize them after the form update. In case of angular components, this issue is solved automatically by not updating them the components. For more info see the `turbo:before-morph-element` eventlistener in the `turbo-global-listeners.ts`.
+
+
+#### How to handle the form preview actions on the backend?
+
+The form preview mechanism shown above can be nicely tied with our existing ActiveRecord object saving services.
+
+1. First, we'll create a `PreviewAttributesService` that inherits from the `SetAttributes` service. This new service is nearly identical to `SetAttributes`, with one key difference: it clears validation errors for fields that the user hasn't modified. This is particularly important when creating new objects. For instance, if the user modifies the first input field, all fields will be validated and errors will be displayed, which is undesirable. Instead, we want to display errors incrementally as the user progresses through the form. With this approach, users will experience instant validation as they complete each field.
+
+ ```ruby
+ module WorkPackages
+ class PreviewAttributesService < ::BaseServices::SetAttributes
+ def perform(*)
+ super.tap do |service_call|
+ clear_unchanged_fields(service_call)
+ end
+ end
+
+ private
+
+ def clear_unchanged_fields(service_call)
+ work_package = service_call.result
+ work_package
+ .errors
+ .select { |error| work_package.changed.exclude?(error.attribute.to_s) }
+ .each do |error|
+ work_package.errors.delete(error.attribute)
+ end
+ end
+ end
+ end
+ ```
+
+2. Then we define a new controller member action called `work_package_form` alongside the crud actions and use the newly defined `PreviewAttributesService`.
+
+ ```ruby
+ class WorkPackagesController < ApplicationController
+ def work_package_form
+ service_call = ::WorkPackages::PreviewAttributesService
+ .new(user: current_user,
+ model: @work_package,
+ contract_class: WorkPackage::UpdateContract)
+ .call(permitted_params.work_package)
+
+ update_via_turbo_stream(
+ component: WorkPackages::EditComponent.new(service_call.result),
+ method: "morph"
+ )
+ # TODO: :unprocessable_entity is not nice, change the dialog logic to accept :ok
+ # without dismissing the dialog, alternatively use turbo frames instead of streams.
+ respond_to_with_turbo_streams(status: :unprocessable_entity)
+
+ end
+ end
+ ```
+ - For a smoother user experience, it is recommended to respond with the `method: "morph"` via turbo streams. This will ensure the user's input focus is maintained between field updates. It is useful for form previews that are triggered on a keystroke event instead of the change event.
+ - The turbo stream response could be replaced with a plain turbo drive html response, once we have the turbo drive morphing enabled.
+ - Responding with a `status: :unprocessable_entity` is also important, because we intend to display validation errors on the form.
+
+3. Having the service and the controller action in place, we can defined form preview path on the form by adding the `"form-preview-url-value": work_packages_form_path(@work_package)` attribute.
### Accessing the form model
diff --git a/lookbook/previews/patterns/forms_preview.rb b/lookbook/previews/patterns/forms_preview.rb
index 0929b50f4e6..d84545474d1 100644
--- a/lookbook/previews/patterns/forms_preview.rb
+++ b/lookbook/previews/patterns/forms_preview.rb
@@ -9,5 +9,16 @@ module Patterns
# @display min_height 300px
# @label Overview
def custom_width_fields_form; end
+
+ # @label Preview
+ # @param answer
+ def form_preview(answer: nil)
+ preview_path =
+ Lookbook::Engine
+ .routes
+ .url_helpers
+ .lookbook_preview_path(path: "patterns/forms/form_preview")
+ render_with_template(locals: { answer:, preview_path: })
+ end
end
end
diff --git a/lookbook/previews/patterns/forms_preview/form_preview.html.erb b/lookbook/previews/patterns/forms_preview/form_preview.html.erb
new file mode 100644
index 00000000000..e148298888d
--- /dev/null
+++ b/lookbook/previews/patterns/forms_preview/form_preview.html.erb
@@ -0,0 +1,35 @@
+<%
+ form_preview = Class.new(ApplicationForm) do
+ form do |f|
+ f.text_field(
+ name: :answer,
+ value: answer,
+ label: "Auto",
+ data: { action: "change->form-preview#submit" },
+ required: true,
+ validation_message: answer.nil? ? "Field is required" : nil,
+ caption: if answer.nil?
+ "Fill in the field to make the validation error instantly go away."
+ else
+ "Yay, no more errors!"
+ end,
+ input_width: :auto
+ )
+ end
+ end
+%>
+
+<%=
+ primer_form_with(
+ url: "/foo",
+ method: :get,
+ data: {
+ "controller": "form-preview",
+ "form-preview-url-value": preview_path
+ }
+ ) do |f|
+%>
+ <%= render(form_preview.new(f)) %>
+<% end %>
+
+
diff --git a/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb b/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
index 506d398e3a7..886b1573bb7 100644
--- a/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
+++ b/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
@@ -6,12 +6,12 @@
model:,
method: :put,
data: {
- "controller": "overview--project-life-cycles-form",
- "overview--project-life-cycles-form-target": "form",
+ "controller": "overview--project-life-cycles-form form-preview",
+ "action": "overview--project-life-cycles-form:triggerFormPreview->form-preview#submit",
+ "form-preview-url-value": project_life_cycles_form_path(project_id: model.id),
"application-target": "dynamic",
turbo: true,
turbo_stream: true,
- preview_url: project_life_cycles_form_path(project_id: model.id),
"test-selector": "async-dialog-content"
},
url: update_project_life_cycles_path(project_id: model.id),