Skip to content

Commit

Permalink
Add validation logic to check if at least one field is filled
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnasambu committed Aug 5, 2024
1 parent 0dd13d3 commit 29112fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/results/result-form-field.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { ZodSchema, z } from "zod";
const isNumeric = (concept: ConceptReference): boolean =>
concept.datatype.display === "Numeric";

const defaultNumericValue = () => 0;

const createResultFormFieldSchema = (
concepts: ConceptReference[],
t: TFunction
Expand All @@ -20,14 +18,27 @@ const createResultFormFieldSchema = (
? z.coerce
.number()
.min(0, { message: t("enterValidNumber", "Enter a valid number") })
.optional()
: z
.string()
.min(1, { message: t("fieldRequired", "This field is required") });
.min(1, { message: t("fieldRequired", "This field is required") })
.optional();

return { ...acc, [concept.uuid]: fieldValidation };
}, {} as Record<string, z.ZodString | z.ZodNumber>);

return z.object(fields);
const schema = z.object(fields).refine(
(data) => {
return Object.values(data).some(
(value) => value !== undefined && value !== null && value !== ""
);
},
{
message: t("atLeastOneField", "At least one field must be filled out"),
}
);

return schema;
};

interface ResultFormFieldProps {
Expand Down Expand Up @@ -74,7 +85,6 @@ const ResultFormField: React.FC<ResultFormFieldProps> = ({
<Controller
control={control}
name={concept.uuid}
rules={{ required: true }}
render={({ field }) => (
<TextInput
key={concept.uuid}
Expand All @@ -99,7 +109,6 @@ const ResultFormField: React.FC<ResultFormFieldProps> = ({
<Controller
name={concept.uuid}
control={control}
rules={{ required: true }}
render={({ field }) => (
<Select
key={concept.uuid}
Expand Down Expand Up @@ -135,7 +144,6 @@ const ResultFormField: React.FC<ResultFormFieldProps> = ({
key={member.uuid}
control={control}
name={member.uuid}
rules={{ required: true }}
render={({ field }) => (
<TextInput
className={styles.textInput}
Expand Down Expand Up @@ -164,7 +172,6 @@ const ResultFormField: React.FC<ResultFormFieldProps> = ({
key={member.uuid}
name={member.uuid}
control={control}
rules={{ required: true }}
render={({ field }) => (
<Select
className={styles.textInput}
Expand Down
15 changes: 14 additions & 1 deletion src/results/result-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {

const submissionDatetime = new Date().toISOString();

const isAtLeastOneFieldFilled = concept.setMembers.some((member) => {
return getValues()[member.uuid];
});

if (!isAtLeastOneFieldFilled) {
setShowErrorNotification(true);
return;
}

if (concept.set && concept.setMembers.length > 0) {
let groupMembers = [];
concept.setMembers.forEach((member) => {
Expand Down Expand Up @@ -267,7 +276,10 @@ const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {
<InlineNotification
lowContrast
title={t("error", "Error")}
subtitle={t("pleaseAllFields", "Please fill all fields") + "."}
subtitle={
t("pleaseFillAtLeastOneField", "Please fill at least one field") +
"."
}
onClose={() => setShowErrorNotification(false)}
/>
</Column>
Expand Down Expand Up @@ -306,3 +318,4 @@ const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {
};

export default ResultForm;
``;

0 comments on commit 29112fe

Please sign in to comment.