From 0c0e3fedf384bd553fd2bc3925105688371d5c13 Mon Sep 17 00:00:00 2001 From: Joshua Toliver Date: Tue, 17 Sep 2024 16:13:42 -0400 Subject: [PATCH 1/2] R2-3005: Validating multiselects with display conditions if display condition is met --- .../record-form/form/record-form.jsx | 2 +- .../record-form/form/validations.js | 34 ++++++++++++++----- .../record-form/form/validations.unit.test.js | 31 +++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/javascript/components/record-form/form/record-form.jsx b/app/javascript/components/record-form/form/record-form.jsx index f8a7775df5..a8f76aa674 100644 --- a/app/javascript/components/record-form/form/record-form.jsx +++ b/app/javascript/components/record-form/form/record-form.jsx @@ -197,7 +197,7 @@ function RecordForm({ return ( <> { validations[name] = bool().oneOf([true], requiredMessage); break; case type === SELECT_FIELD && multiSelect: - validations[name] = array().min(1, requiredMessage); + validations[name] = array(); break; default: validations[name] = (validations[name] || string()).nullable(); @@ -162,14 +162,26 @@ export const fieldValidations = (field, { i18n, online = false }) => { [relatedField]: relatedFieldValue }); }, - then: - type !== TALLY_FIELD - ? schema.required(requiredMessage) - : currentSchema => - currentSchema.test(name, requiredMessage, value => { - return compact(Object.values(value)).length > 0; - }), - otherwise: type !== TALLY_FIELD ? schema.notRequired() : currentSchema => currentSchema.notRequired() + then: $schema => { + if (type === SELECT_FIELD && multiSelect) { + return $schema.min(1, requiredMessage); + } + + if (type === TALLY_FIELD) { + return $schema.test(name, requiredMessage, value => { + return compact(Object.values(value)).length > 0; + }); + } + + return $schema.required(requiredMessage); + }, + otherwise: $schema => { + if (type === SELECT_FIELD && multiSelect) { + return $schema.min(0); + } + + return $schema.notRequired(); + } }); } else { if (!([TICK_FIELD, SELECT_FIELD, TALLY_FIELD].includes(type) || (type !== SELECT_FIELD && multiSelect))) { @@ -180,6 +192,10 @@ export const fieldValidations = (field, { i18n, online = false }) => { validations[name] = schema.required(requiredMessage); } + if (type === SELECT_FIELD && multiSelect) { + validations[name] = schema.min(1, requiredMessage).required(requiredMessage); + } + if (type === TALLY_FIELD) { validations[name] = schema.test(name, requiredMessage, value => { return compact(Object.values(value)).length > 0; diff --git a/app/javascript/components/record-form/form/validations.unit.test.js b/app/javascript/components/record-form/form/validations.unit.test.js index 08c664cf49..a2358978e4 100644 --- a/app/javascript/components/record-form/form/validations.unit.test.js +++ b/app/javascript/components/record-form/form/validations.unit.test.js @@ -140,6 +140,37 @@ describe("/form/validations", () => { expect(schema.isValidSync(formData)).to.be.false; }); + + describe("select with a display condition", () => { + const selectFieldWithDisplayCondition = { + name: "cities", + display_name: { en: "Cities" }, + type: SELECT_FIELD, + multi_select: true, + required: true, + display_conditions_record: { + eq: { testField: 1 } + } + }; + + it("should not validate", () => { + const schema = object().shape( + validations.fieldValidations({ ...selectFieldWithDisplayCondition, required: true }, { i18n }) + ); + const formData = {}; + + expect(schema.isValidSync(formData)).to.be.true; + }); + + it("should validate", () => { + const schema = object().shape( + validations.fieldValidations({ ...selectFieldWithDisplayCondition, required: true }, { i18n }) + ); + const formData = { testField: 1 }; + + expect(schema.isValidSync(formData)).to.be.true; + }); + }); }); context("when it is not required", () => { From ba9d402413f19dd7045c8994bdf17d6a870bc288 Mon Sep 17 00:00:00 2001 From: Joshua Toliver Date: Tue, 17 Sep 2024 16:15:53 -0400 Subject: [PATCH 2/2] Removing unused code --- app/javascript/components/record-form/form/record-form.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/components/record-form/form/record-form.jsx b/app/javascript/components/record-form/form/record-form.jsx index a8f76aa674..83757c45db 100644 --- a/app/javascript/components/record-form/form/record-form.jsx +++ b/app/javascript/components/record-form/form/record-form.jsx @@ -197,7 +197,7 @@ function RecordForm({ return ( <>