Skip to content

Commit

Permalink
O3-2057 Add support to display error messages on Number input control (
Browse files Browse the repository at this point in the history
…#22)

* add error message for min and max on number field

* refactor code

* more code refactor

* remove number conversion on value
  • Loading branch information
kajambiya authored Apr 19, 2023
1 parent 8e008c7 commit 15486b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/components/encounter/ohri-encounter-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
},
};
}

if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) {
const ac = new AbortController();
return saveEncounter(ac, encounterForSubmission, encounter?.uuid);
Expand Down
26 changes: 26 additions & 0 deletions src/validators/ohri-form-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 [];
},
};
Expand Down

0 comments on commit 15486b8

Please sign in to comment.