diff --git a/app/javascript/components/record-form/form/validations.js b/app/javascript/components/record-form/form/validations.js index 4b66925ca5..2111d6d501 100644 --- a/app/javascript/components/record-form/form/validations.js +++ b/app/javascript/components/record-form/form/validations.js @@ -164,7 +164,7 @@ export const fieldValidations = (field, { i18n, online = false }) => { }, then: $schema => { if (type === SELECT_FIELD && multiSelect) { - return $schema.min(1, requiredMessage); + return $schema.min(1, requiredMessage).default([]); } if (type === TALLY_FIELD) { @@ -193,7 +193,7 @@ export const fieldValidations = (field, { i18n, online = false }) => { } if (type === SELECT_FIELD && multiSelect) { - validations[name] = schema.min(1, requiredMessage).required(requiredMessage); + validations[name] = schema.min(1, requiredMessage).required(requiredMessage).default([]); } if (type === TALLY_FIELD) { 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 a2358978e4..aa3b1a1aaf 100644 --- a/app/javascript/components/record-form/form/validations.unit.test.js +++ b/app/javascript/components/record-form/form/validations.unit.test.js @@ -120,18 +120,19 @@ describe("/form/validations", () => { required: true }; - it("should not be valid if it is empty", () => { + it("should not be valid if it is empty array", () => { const schema = object().shape(validations.fieldValidations({ ...selectField, required: true }, { i18n })); const formData = { cities: [] }; expect(schema.isValidSync(formData)).to.be.false; }); - it("should not be valid if it is null", () => { + it("should not be valid if it is null or undefined", () => { const schema = object().shape(validations.fieldValidations({ ...selectField, required: true }, { i18n })); - const formData = { cities: null }; - expect(schema.isValidSync(formData)).to.be.false; + expect(schema.isValidSync({ cities: null })).to.be.false; + expect(schema.isValidSync({ cities: undefined })).to.be.false; + expect(schema.isValidSync({})).to.be.false; }); it("should not be valid if it is not present", () => { @@ -168,7 +169,7 @@ describe("/form/validations", () => { ); const formData = { testField: 1 }; - expect(schema.isValidSync(formData)).to.be.true; + expect(schema.isValidSync(formData)).to.be.false; }); }); });