Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add form reset hooks to category editor (#38945)

Sérgio Saquetimc12e7a212c18
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index a0bae9c0eae..fb05648b2df 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -117,6 +117,13 @@ export default class EditCategoryTabsController extends Controller {
@and("showTooltip", "model.cannot_delete_reason") showDeleteReason;
+ /**
+ * Callbacks registered by tab components that are invoked when the form
+ * is reset, allowing child components to clean up their own state.
+ * @type {Function[]}
+ */
+ afterResetCallbacks = [];
+
@action
initFormData() {
const enableSimplifiedCategoryCreation =
@@ -219,12 +226,27 @@ export default class EditCategoryTabsController extends Controller {
this.showAdvancedTabs = this.showAdvancedTabs || tab !== "general";
}
+ /**
+ * Runs all registered validators, then performs built-in validation for
+ * required fields (name, emoji, icon) when submitting from a non-general tab.
+ * Both `addError` and `removeError` are passed to validators so they can
+ * manage errors bidirectionally.
+ *
+ * @param {Object} data - The current form draft data.
+ * @param {Object} helpers
+ * @param {Function} helpers.addError - Adds a validation error for a field.
+ * @param {Function} helpers.removeError - Removes a validation error for a field.
+ */
@action
- validateForm(data, { addError }) {
+ validateForm(data, { addError, removeError }) {
if (!this.siteSettings.enable_simplified_category_creation) {
return;
}
+ for (const validator of this.validators) {
+ validator(data, { addError, removeError });
+ }
+
if (this.selectedTab === "general") {
return;
}
@@ -265,6 +287,27 @@ export default class EditCategoryTabsController extends Controller {
this.validators.push(validator);
}
+ /**
+ * Registers a callback that will be invoked when the form is reset.
+ * Tab components use this to synchronize their internal state (e.g.,
+ * clearing local selections) when the user resets the form.
+ *
+ * @param {Function} callback - The function to call on form reset.
+ */
+ @action
+ registerAfterReset(callback) {
+ this.afterResetCallbacks.push(callback);
+ }
+
+ /**
+ * Called by FormKit's `@onReset` hook. Invokes all registered
+ * after-reset callbacks so tab components can react to the reset.
+ */
+ @action
+ onFormReset() {
+ this.afterResetCallbacks.forEach((callback) => callback());
+ }
+
@action
isLeavingForm(transition) {
const name = transition.targetName;
diff --git a/frontend/discourse/admin/templates/edit-category/tabs.gjs b/frontend/discourse/admin/templates/edit-category/tabs.gjs
index 43743728b1e..4ab7d827008 100644
--- a/frontend/discourse/admin/templates/edit-category/tabs.gjs
+++ b/frontend/discourse/admin/templates/edit-category/tabs.gjs
@@ -74,6 +74,7 @@ export default <template>
@onSubmit={{@controller.saveCategory}}
@validate={{@controller.validateForm}}
@onRegisterApi={{@controller.onRegisterFormApi}}
+ @onReset={{@controller.onFormReset}}
as |form transientData|
>
<form.Section
@@ -91,6 +92,7 @@ export default <template>
@selectedTab={{@controller.selectedTab}}
@category={{@controller.model}}
@registerValidator={{@controller.registerValidator}}
+ @registerAfterReset={{@controller.registerAfterReset}}
@transientData={{transientData}}
@form={{form}}
@setSelectedTab={{@controller.setSelectedTab}}
@@ -109,6 +111,7 @@ export default <template>
@selectedTab={{@controller.selectedTab}}
@category={{@controller.model}}
@registerValidator={{@controller.registerValidator}}
+ @registerAfterReset={{@controller.registerAfterReset}}
@transientData={{transientData}}
@form={{form}}
/>
diff --git a/frontend/discourse/tests/unit/controllers/edit-category-tabs-test.js b/frontend/discourse/tests/unit/controllers/edit-category-tabs-test.js
new file mode 100644
index 00000000000..a78d1523e89
--- /dev/null
+++ b/frontend/discourse/tests/unit/controllers/edit-category-tabs-test.js
@@ -0,0 +1,95 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+
+module("Unit | Controller | edit-category-tabs", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.controller = getOwner(this).lookup("controller:edit-category/tabs");
+ });
+
+ test("registerAfterReset stores callbacks", function (assert) {
+ let called = false;
+ this.controller.registerAfterReset(() => (called = true));
+
+ assert.strictEqual(
+ this.controller.afterResetCallbacks.length,
+ 1,
+ "callback is registered"
+ );
+
+ this.controller.onFormReset();
+ assert.true(called, "callback is invoked on form reset");
+ });
+
+ test("onFormReset invokes all registered callbacks", function (assert) {
+ const log = [];
+ this.controller.registerAfterReset(() => log.push("a"));
+ this.controller.registerAfterReset(() => log.push("b"));
+
+ this.controller.onFormReset();
+
+ assert.deepEqual(log, ["a", "b"], "all callbacks are invoked in order");
+ });
+
+ test("validateForm delegates to registered validators", function (assert) {
+ this.controller.siteSettings = {
+ enable_simplified_category_creation: true,
+ };
+ this.controller.selectedTab = "general";
+
+ const validatorLog = [];
+ this.controller.registerValidator((data, { addError, removeError }) => {
+ validatorLog.push({
+ data,
+ hasAddError: !!addError,
+ hasRemoveError: !!removeError,
+ });
+ });
+
+ const data = { name: "Test" };
+ const addError = () => {};
+ const removeError = () => {};
+
+ this.controller.validateForm(data, { addError, removeError });
+
+ assert.strictEqual(validatorLog.length, 1, "validator was called");
+ assert.deepEqual(
+ validatorLog[0].data,
+ data,
+ "validator receives the form data"
+ );
+ assert.true(
+ validatorLog[0].hasAddError,
+ "validator receives addError helper"
+ );
+ assert.true(
+ validatorLog[0].hasRemoveError,
+ "validator receives removeError helper"
+ );
+ });
+
+ test("validateForm passes removeError to validators", function (assert) {
+ this.controller.siteSettings = {
+ enable_simplified_category_creation: true,
+ };
+ this.controller.selectedTab = "general";
+
+ let removedField;
+ this.controller.registerValidator((_data, { removeError }) => {
+ removeError("myField");
+ });
+
+ const addError = () => {};
+ const removeError = (name) => (removedField = name);
+
+ this.controller.validateForm({ name: "Test" }, { addError, removeError });
+
+ assert.strictEqual(
+ removedField,
+ "myField",
+ "validator can call removeError"
+ );
+ });
+});