A platform for community discussion. Free, open, simple.
FIX: Enforce `required` validation on form template upload field (#38799)
Régis Hanol09a98ae92238
diff --git a/app/assets/stylesheets/common/components/form-template-field.scss b/app/assets/stylesheets/common/components/form-template-field.scssindex b2d0f9ad6ea..3e7156fecd2 100644--- a/app/assets/stylesheets/common/components/form-template-field.scss+++ b/app/assets/stylesheets/common/components/form-template-field.scss@@ -70,6 +70,12 @@color: var(--primary-medium);}+ &__upload-hidden-input {+ position: absolute;+ opacity: 0;+ pointer-events: none;+ }+&__composer {// same default effective height (150px) than a text controlheight: 189px;diff --git a/frontend/discourse/app/components/form-template-field/upload.gjs b/frontend/discourse/app/components/form-template-field/upload.gjsindex 054ca77fa4f..8964f2b711b 100644--- a/frontend/discourse/app/components/form-template-field/upload.gjs+++ b/frontend/discourse/app/components/form-template-field/upload.gjs@@ -110,6 +110,9 @@ export default class FormTemplateFieldUpload extends Component {next(this, () => {this.args.onChange(this.uploadValue);+ document+ .querySelector(`input[name="${this.args.id}"]`)+ ?.dispatchEvent(new Event("input", { bubbles: true }));});}@@ -146,8 +149,6 @@ export default class FormTemplateFieldUpload extends Component {</span>{{/if}}- <input type="hidden" name={{@id}} value={{this.uploadValue}} />-<PickFilesButton@registerFileInput={{this.uppyUpload.setup}}@fileInputClass="form-template-field__upload"@@ -177,6 +178,16 @@ export default class FormTemplateFieldUpload extends Component {{{/each}}</ul>{{/if}}++ <input+ type="text"+ name={{@id}}+ value={{this.uploadValue}}+ required={{if @validations.required "required" ""}}+ class="form-template-field__upload-hidden-input"+ tabindex="-1"+ aria-hidden="true"+ /></div></template>}diff --git a/frontend/discourse/tests/integration/components/form-template-field/upload-test.gjs b/frontend/discourse/tests/integration/components/form-template-field/upload-test.gjsnew file mode 100644index 00000000000..73c488dc942--- /dev/null+++ b/frontend/discourse/tests/integration/components/form-template-field/upload-test.gjs@@ -0,0 +1,48 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import FormUpload from "discourse/components/form-template-field/upload";+import noop from "discourse/helpers/noop";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++module(+ "Integration | Component | form-template-field | upload",+ function (hooks) {+ setupRenderingTest(hooks);++ test("sets required attribute when validation is required", async function (assert) {+ const attributes = {};+ const validations = { required: true };++ await render(+ <template>+ <FormUpload+ @id="test-upload"+ @attributes={{attributes}}+ @validations={{validations}}+ @onChange={{noop}}+ />+ </template>+ );++ assert.dom("input[name='test-upload']").hasAttribute("required");+ });++ test("does not set required attribute when validation is not required", async function (assert) {+ const attributes = {};+ const validations = {};++ await render(+ <template>+ <FormUpload+ @id="test-upload"+ @attributes={{attributes}}+ @validations={{validations}}+ @onChange={{noop}}+ />+ </template>+ );++ assert.dom("input[name='test-upload']").doesNotHaveAttribute("required");+ });+ }+);diff --git a/spec/system/composer/template_validation_spec.rb b/spec/system/composer/template_validation_spec.rbindex 65516cfa290..3e7c90a849f 100644--- a/spec/system/composer/template_validation_spec.rb+++ b/spec/system/composer/template_validation_spec.rb@@ -129,6 +129,39 @@ describe "Composer Form Template Validations" do)end+ it "shows an error when a required upload field has no file" do+ form_template_with_upload =+ Fabricate(+ :form_template,+ name: "Upload Required",+ template:+ %Q(+ - type: upload+ id: my-upload+ attributes:+ file_types: ".jpg, .png"+ label: "Upload a file"+ validations:+ required: true),+ )+ category =+ Fabricate(+ :category,+ name: "Uploads",+ slug: "uploads",+ form_template_ids: [form_template_with_upload.id],+ )++ category_page.visit(category)+ category_page.new_topic_button.click+ composer.fill_title(topic_title)+ composer.create++ expect(composer).to have_form_template_field_error(+ I18n.t("js.form_templates.errors.value_missing.default"),+ )+ end+it "shows an error when an input doesn't satisfy the requested pattern" docategory_page.visit(category_with_template_2)category_page.new_topic_button.click