Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Year Of Birth Field Validation Condition in Patient transfer form #9133

48 changes: 34 additions & 14 deletions src/components/Facility/TransferPatientDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,38 @@ const TransferPatientDialog = (props: Props) => {
const maxYear = new Date().getFullYear();

const handleChange = (e: FieldChangeEvent<unknown>) => {
if (
e.name === "year_of_birth" &&
parseInt((e.value as string) || "0") > maxYear
) {
const value = String(e.value);

if (e.name === "year_of_birth") {
if (value.length <= 4) {
dispatch({
type: "set_form",
form: { ...state.form, [e.name]: e.value },
});
}
} else {
dispatch({
type: "set_error",
errors: {
...state.errors,
[e.name]: `Cannot be greater than ${maxYear}`,
},
type: "set_form",
form: { ...state.form, [e.name]: e.value },
});
return;
}
};

const handleOnBlur = (e: React.FocusEvent<HTMLInputElement>) => {
const yearValue = Number(state.form.year_of_birth);
if (!state.form.year_of_birth) return;
let errorMessage = "";
if (yearValue > maxYear) {
errorMessage = `Cannot be greater than ${maxYear}`;
} else if (yearValue < 1900) {
errorMessage = `Cannot be smaller than 1900`;
}
dispatch({
type: "set_form",
form: { ...state.form, [e.name]: e.value },
type: "set_error",
errors: {
...state.errors,
[e.target.name]: errorMessage,
},
});
};

Expand All @@ -115,6 +131,11 @@ const TransferPatientDialog = (props: Props) => {
errors[field] = `Cannot be greater than ${maxYear}`;
invalidForm = true;
}

if (parseInt(state.form[field] || "0") < 1900) {
errors[field] = `Cannot be smaller than 1900`;
invalidForm = true;
}
return;
default:
return;
Expand Down Expand Up @@ -193,9 +214,8 @@ const TransferPatientDialog = (props: Props) => {
label="Year of birth"
labelClassName="text-sm"
value={state.form.year_of_birth}
min="1900"
max={maxYear}
onChange={handleChange}
onBlur={handleOnBlur}
placeholder="Enter year of birth"
error={state.errors.year_of_birth}
/>
Expand Down
Loading