From 15486b892492a7b44a0faa1fbcc9097b9d2e6300 Mon Sep 17 00:00:00 2001 From: Jovan Ssebaggala Date: Wed, 19 Apr 2023 11:23:05 +0300 Subject: [PATCH] O3-2057 Add support to display error messages on Number input control (#22) * add error message for min and max on number field * refactor code * more code refactor * remove number conversion on value --- .../encounter/ohri-encounter-form.tsx | 1 + src/validators/ohri-form-validator.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/components/encounter/ohri-encounter-form.tsx b/src/components/encounter/ohri-encounter-form.tsx index 86e0680dd..a53c584f4 100644 --- a/src/components/encounter/ohri-encounter-form.tsx +++ b/src/components/encounter/ohri-encounter-form.tsx @@ -402,6 +402,7 @@ export const OHRIEncounterForm: React.FC = ({ }, }; } + if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) { const ac = new AbortController(); return saveEncounter(ac, encounterForSubmission, encounter?.uuid); diff --git a/src/validators/ohri-form-validator.ts b/src/validators/ohri-form-validator.ts index a14da9db1..22687abd4 100644 --- a/src/validators/ohri-form-validator.ts +++ b/src/validators/ohri-form-validator.ts @@ -2,6 +2,7 @@ import { FieldValidator, OHRIFormField } from '../api/types'; import { isTrue } from '../utils/boolean-utils'; export const fieldRequiredErrCode = 'field.required'; +export const fieldOutOfBoundErrCode = 'field.outOfBound'; export const OHRIFieldValidator: FieldValidator = { validate: (field: OHRIFormField, value: any) => { @@ -13,6 +14,31 @@ export const OHRIFieldValidator: FieldValidator = { return [{ resultType: 'error', errCode: fieldRequiredErrCode, message: 'Field is mandatory' }]; } } + if (field.questionOptions.rendering == 'number') { + const min = field.questionOptions.min; + const max = field.questionOptions.max; + if (min && value < Number(min)) { + return [ + { + resultType: 'error', + errCode: fieldOutOfBoundErrCode, + // TODO: handle i18n + message: `Field value can't be less than ${min}`, + }, + ]; + } + + if (max && value > Number(max)) { + return [ + { + resultType: 'error', + errCode: fieldOutOfBoundErrCode, + // TODO: handle i18n + message: `Field value can't be greater than ${max}`, + }, + ]; + } + } return []; }, };