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

update Patient validation to use configurable regex #1611

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions frontend/src/components/patient/CreatePatientForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function CreatePatientForm(props) {
const [healthDistricts, setHealthDistricts] = useState([]);
const [educationList, setEducationList] = useState([]);
const [maritalStatuses, setMaritalStatuses] = useState([]);
const [prevfirstName, setPrevfirstName] = useState("");
const [prevlastName, setPrevlastName] = useState("");
const [prevfirstContactName, setPrevfirstContactName] = useState("");
const [prevlastContactName, setPrevlastContactName] = useState("");
const [formAction, setFormAction] = useState("ADD");
const [dateOfBirthFormatter, setDateOfBirthFormatter] = useState({
years: "",
Expand Down Expand Up @@ -206,17 +210,55 @@ function CreatePatientForm(props) {
};

function handleFirstNameChange(event) {
const regex = /^[A-Za-z]*$/;
if (!regex.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^A-Za-z]/g, "");
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.FIRST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevfirstName;
}
setPrevfirstName(event.target.value);
}

function handleLastNameChange(event) {
const regex = /^[A-Za-z]*$/;
if (!regex.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^A-Za-z]/g, "");
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.LAST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevlastName;
}
setPrevlastName(event.target.value);
}

function handleFirstContactNameChange(event) {
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.FIRST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevfirstContactName;
}
setPrevfirstContactName(event.target.value);
}

function handleLastContactNameChange(event) {
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.LAST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevlastContactName;
}
setPrevlastContactName(event.target.value);
}

function fetchHealthDistrictsCallback(res) {
Expand Down Expand Up @@ -731,6 +773,7 @@ function CreatePatientForm(props) {
id: "patientcontact.person.lastname",
})}
id={field.name}
onChange={(e) => handleLastContactNameChange(e)}
placeholder={intl.formatMessage({
id: "patient.emergency.lastname",
})}
Expand All @@ -750,6 +793,7 @@ function CreatePatientForm(props) {
id: "patientcontact.person.firstname",
})}
id={field.name}
onChange={(e) => handleFirstContactNameChange(e)}
placeholder={intl.formatMessage({
id: "patient.emergency.firstname",
})}
Expand Down
34 changes: 24 additions & 10 deletions frontend/src/components/patient/SearchPatientForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ function SearchPatientForm(props) {
const [searchFormValues, setSearchFormValues] = useState(
SearchPatientFormValues,
);
const [prevfirstName, setPrevfirstName] = useState("");
const [prevlastName, setPrevlastName] = useState("");

const handlePatientImport = (patientId) => {
console.log("Import button clicked, patientId:", patientId);
Expand Down Expand Up @@ -231,19 +233,31 @@ function SearchPatientForm(props) {
setDob(date);
};

const handleFirstNameChange = (event) => {
const regex = /^[A-Za-z]*$/;
if (!regex.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^A-Za-z]/g, "");
function handleFirstNameChange(event) {
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.FIRST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevfirstName;
}
};
setPrevfirstName(event.target.value);
}

const handleLastNameChange = (event) => {
const regex = /^[A-Za-z]*$/;
if (!regex.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^A-Za-z]/g, "");
function handleLastNameChange(event) {
const regexFlags = "iu";
const regex = new RegExp(
configurationProperties.LAST_NAME_REGEX,
regexFlags,
);
const value = event.target.value;
if (!regex.test(value)) {
event.target.value = prevlastName;
}
};
setPrevlastName(event.target.value);
}

const patientSelected = (e) => {
const patientSelected = patientSearchResults.find((patient) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public class DisplayListController extends BaseRestController {
protected static List<Integer> statusList;
protected static List<String> nfsTestIdList;

private String escapeRegexChars(String regex) {
// TODO Auto-generated method stub
return regex;
}

// Manually create an instance of ExportTrendsByDate
private ExportTrendsByDate exportTrendsByDate = new ExportTrendsByDate();

Expand Down Expand Up @@ -352,6 +357,15 @@ private List<IdValuePair> getSiteNameList() {
@GetMapping(value = "configuration-properties", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
private Map<String, Object> getConfigurationProperties() {
SiteInformation DEFAULT_SITE_INFORATION = new SiteInformation();
String DEFAULT_REGEX = "0-9a-z .'_@-";
DEFAULT_SITE_INFORATION.setValue(DEFAULT_REGEX);
String FIRST_NAME_REGEX = "^[" + escapeRegexChars(
siteInformationService.getMatch("name", "firstNameCharset").orElse(DEFAULT_SITE_INFORATION).getValue())
+ "]*$";
String LAST_NAME_REGEX = "^[" + escapeRegexChars(
siteInformationService.getMatch("name", "lastNameCharset").orElse(DEFAULT_SITE_INFORATION).getValue())
+ "]*$";
Map<String, Object> configs = getOpenConfigurationProperties();

configs.put(Property.allowResultRejection.toString(),
Expand All @@ -369,6 +383,8 @@ private Map<String, Object> getConfigurationProperties() {
ConfigurationProperties.getInstance().getPropertyValue(Property.UseExternalPatientInfo));
configs.put("DEFAULT_PAGE_SIZE",
ConfigurationProperties.getInstance().getPropertyValue("page.defaultPageSize"));
configs.put("FIRST_NAME_REGEX", FIRST_NAME_REGEX);
configs.put("LAST_NAME_REGEX", LAST_NAME_REGEX);
return configs;
}

Expand Down