From 6d179745ec64704f73c9b74a00d6bdaae38ea574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=94=A6=E5=BF=83?= <41134017+Lhcfl@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:09:48 +0800 Subject: [PATCH] FIX: Empty param-inout form should reject submit (#309) The `onSubmit` hook will only be triggered when the form is valid, so we need to clear the contents of `serializedData` in advance in `submit`. Otherwise, it may not throw a validation error. --- .../discourse/components/param-input-form.gjs | 2 +- .../controllers/group-reports-show.js | 2 +- .../components/param-input-test.js | 32 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/components/param-input-form.gjs b/assets/javascripts/discourse/components/param-input-form.gjs index 21da0cb..1a58998 100644 --- a/assets/javascripts/discourse/components/param-input-form.gjs +++ b/assets/javascripts/discourse/components/param-input-form.gjs @@ -278,6 +278,7 @@ export default class ParamInputForm extends Component { if (this.form == null) { throw "No form"; } + this.serializedData = null; await this.form.submit(); if (this.serializedData == null) { throw new ParamValidationError("validation_failed"); @@ -293,7 +294,6 @@ export default class ParamInputForm extends Component { @action onSubmit(data) { - this.serializedData = null; const serializedData = {}; for (const [id, val] of Object.entries(data)) { serializedData[id] = diff --git a/assets/javascripts/discourse/controllers/group-reports-show.js b/assets/javascripts/discourse/controllers/group-reports-show.js index 5f222c7..d429dd3 100644 --- a/assets/javascripts/discourse/controllers/group-reports-show.js +++ b/assets/javascripts/discourse/controllers/group-reports-show.js @@ -91,7 +91,7 @@ export default class GroupReportsShowController extends Controller { } catch (error) { if (error.jqXHR?.status === 422 && error.jqXHR.responseJSON) { this.results = error.jqXHR.responseJSON; - } else if (error instanceof ParamValidationError) { + } else if (!(error instanceof ParamValidationError)) { popupAjaxError(error); } } finally { diff --git a/test/javascripts/components/param-input-test.js b/test/javascripts/components/param-input-test.js index 2937708..a116e28 100644 --- a/test/javascripts/components/param-input-test.js +++ b/test/javascripts/components/param-input-test.js @@ -169,4 +169,36 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) { }); } } + + test("empty form will reject submit", async function (assert) { + this.setProperties({ + param_info: [ + { + identifier: "string", + type: "string", + default: null, + nullable: false, + }, + ], + initialValues: {}, + onRegisterApi: ({ submit }) => { + this.submit = submit; + }, + }); + + await render(hbs` + `); + + assert.rejects(this.submit()); + + // After successfully submitting the test once, edit and submit again. + await fillIn(`[name="string"]`, "foo"); + await this.submit(); + await fillIn(`[name="string"]`, ""); + assert.rejects(this.submit()); + }); });