Skip to content

Commit

Permalink
Merge pull request #1380 from harshitg927/issue-1375
Browse files Browse the repository at this point in the history
Fix: Prevent negative age input values in New Patient form, along wit…
  • Loading branch information
mozzy11 authored Jan 9, 2025
2 parents 79c7573 + 6b52f5f commit e0a6881
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
6 changes: 3 additions & 3 deletions frontend/cypress/pages/ModifyOrderPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class ModifyOrderPage {

clickRespectivePatient() {
return cy
.get('tbody tr')
.first()
.find('.cds--radio-button__appearance')
.get("tbody tr")
.first()
.find(".cds--radio-button__appearance")
.click();
}
}
Expand Down
39 changes: 33 additions & 6 deletions frontend/src/components/patient/CreatePatientForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,16 @@ function CreatePatientForm(props) {
};

function handleYearsChange(e, values) {
setPatientDetails(values);
let years = e.target.value;
// Ensure years is not negative
const years = Math.max(0, Number(e.target.value));

// Update form values with the validated years
setPatientDetails({
...values,
// Update the specific field that contains years to ensure the form shows the corrected value
[e.target.name]: years,
});

let dobFormatter = {
...dateOfBirthFormatter,
years: years,
Expand All @@ -153,8 +161,16 @@ function CreatePatientForm(props) {
}

function handleMonthsChange(e, values) {
setPatientDetails(values);
let months = e.target.value;
// Ensure months is not negative
const months = Math.max(0, Number(e.target.value));

// Update form values with the validated months
setPatientDetails({
...values,
// Update the specific field that contains months to ensure the form shows the corrected value
[e.target.name]: months,
});

let dobFormatter = {
...dateOfBirthFormatter,
months: months,
Expand All @@ -163,8 +179,16 @@ function CreatePatientForm(props) {
}

function handleDaysChange(e, values) {
setPatientDetails(values);
let days = e.target.value;
// Ensure days is not negative
const days = Math.max(0, Number(e.target.value));

// Update form values with the validated days
setPatientDetails({
...values,
// Update the specific field that contains days to ensure the form shows the corrected value
[e.target.name]: days,
});

let dobFormatter = {
...dateOfBirthFormatter,
days: days,
Expand Down Expand Up @@ -625,6 +649,7 @@ function CreatePatientForm(props) {
})}
id="years"
type="number"
min="0"
onChange={(e) => handleYearsChange(e, values)}
placeholder={intl.formatMessage({
id: "patient.information.age",
Expand All @@ -637,6 +662,7 @@ function CreatePatientForm(props) {
name="months"
labelText={intl.formatMessage({ id: "patient.age.months" })}
type="number"
min="0"
onChange={(e) => handleMonthsChange(e, values)}
id="months"
placeholder={intl.formatMessage({
Expand All @@ -649,6 +675,7 @@ function CreatePatientForm(props) {
value={dateOfBirthFormatter.days}
name="days"
type="number"
min="0"
onChange={(e) => handleDaysChange(e, values)}
labelText={intl.formatMessage({ id: "patient.age.days" })}
id="days"
Expand Down

0 comments on commit e0a6881

Please sign in to comment.