From 0aa6de6eb842e5dd5f9c0557acb324ca11c29873 Mon Sep 17 00:00:00 2001 From: Brendan Bond Date: Wed, 23 Oct 2024 09:58:50 -0500 Subject: [PATCH] Merge pull request #178 from formio/FIO-9244-Radio-component-with-Allow-only-available-values-checked-does-not-submit FIO-9244: fixed an issue where Radio component with Allow only available values checked does not submit --- .../__tests__/validateAvailableItems.test.ts | 28 ++++++++++++------- .../rules/validateAvailableItems.ts | 26 ++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateAvailableItems.test.ts b/src/process/validation/rules/__tests__/validateAvailableItems.test.ts index cc5beaba..a227646c 100644 --- a/src/process/validation/rules/__tests__/validateAvailableItems.test.ts +++ b/src/process/validation/rules/__tests__/validateAvailableItems.test.ts @@ -473,11 +473,15 @@ it('Validating a simple radio component with url data source with the available }; const context = generateProcessorContext(component, data); - context.fetch = (url: string, options?: RequestInit | undefined) => { - return Promise.resolve({ - ok: true, - json: () => Promise.resolve(['1', '2', '3']) - }); + context.fetch = () => { + return Promise.resolve({ + ok: true, + json: () => Promise.resolve([ + { label: '1', value: '1' }, + { label: '2', value: '2' }, + { label: '3', value: '3' }, + ]), + }); }; const result = await validateAvailableItems(context); expect(result).to.equal(null); @@ -498,11 +502,15 @@ it('Validating a simple radio component with url data source with the available }; const context = generateProcessorContext(component, data); - context.fetch = (url: string, options?: RequestInit | undefined) => { - return Promise.resolve({ - ok: true, - json: () => Promise.resolve(['1', '2', '3']) - }); + context.fetch = () => { + return Promise.resolve({ + ok: true, + json: () => Promise.resolve([ + { label: '1', value: '1' }, + { label: '2', value: '2' }, + { label: '3', value: '3' }, + ]), + }); }; const result = await validateAvailableItems(context); expect(result).to.be.instanceOf(FieldError); diff --git a/src/process/validation/rules/validateAvailableItems.ts b/src/process/validation/rules/validateAvailableItems.ts index 64c361b9..2be1a732 100644 --- a/src/process/validation/rules/validateAvailableItems.ts +++ b/src/process/validation/rules/validateAvailableItems.ts @@ -230,16 +230,22 @@ export const validateAvailableItems: RuleFn = async (context: ValidationContext) return null; } - const values = component.dataSrc === 'url' ? await getAvailableDynamicValues(component, context) : component.values; - if (values) { - if (isObject(value)) { - return values.find((optionValue) => compareComplexValues(optionValue, value, context)) !== - undefined - ? null - : error; - } - return values.find((optionValue) => optionValue === value) !== undefined ? null : error; - } + const values = + component.dataSrc === 'url' + ? await getAvailableDynamicValues(component, context) + : component.values; + if (values) { + if (isObject(value)) { + return values.find((optionValue) => compareComplexValues(optionValue, value, context)) !== + undefined + ? null + : error; + } + return values.find((optionValue) => optionValue.value === value || optionValue === value) !== + undefined + ? null + : error; + } return null; } else if (isValidateableSelectComponent(component)) {