Skip to content

Fix: auto formatting of patient DOB search field is too aggresive#142

Open
LiamStanziani wants to merge 1 commit into
MagentaHealth:release/2026-03-17from
openo-beta:bug/auto-formatting-patient-dob-search-too-aggressive-base-release
Open

Fix: auto formatting of patient DOB search field is too aggresive#142
LiamStanziani wants to merge 1 commit into
MagentaHealth:release/2026-03-17from
openo-beta:bug/auto-formatting-patient-dob-search-too-aggressive-base-release

Conversation

@LiamStanziani
Copy link
Copy Markdown
Collaborator

@LiamStanziani LiamStanziani commented Jun 5, 2026

Summary

Fixes the patient DOB search so wildcard (%) searches work again, and so
editing the query no longer yanks the cursor to the end of the field. Touches
the search box (zdemographicfulltitlesearch.jsp) and the search controller
(demographiccontrol.jsp).

Original PR: openo-beta#2450

Problem

The DOB search field auto-formats input as yyyy-mm-dd, but the formatting was
too aggressive and caused two regressions:

  1. Wildcards were stripped. formatDateInput ran value.replace(/\D/g, '')
    on every keystroke, deleting the % character outright — so a search like
    2024-07-% (everyone born July 2024) was impossible; the % vanished as you
    typed. checkTypeIn then also required a full 8-digit date, rejecting any
    partial/wildcard entry.
  2. The cursor jumped to the end on every edit. formatDateInput reassigned
    input.value on each keystroke without restoring the caret, so editing from
    the middle of the text (e.g. changing 20262025) bounced the cursor to
    the end and made mid-field edits painful.

Separately, the controller URL-encoded the keyword
(URLEncoder.encode(keyword, "UTF-8")), which turned % into %25 and
corrupted other legitimate characters (e.g. apostrophes in names), further
breaking wildcard matching.

Solution

Frontend (zdemographicfulltitlesearch.jsp):

  • Rewrote formatDateInput to keep digits, hyphens, and %. It now auto-inserts
    hyphens only when a plain date is typed at the end of the field, and it
    preserves the caret on mid-field edits instead of forcing it to the end.
  • Rewrote checkTypeIn so a % triggers a wildcard search: the value is padded
    to three hyphen-separated segments (2024-072024-07-%, 2024
    2024-%-%) to match what the backend expects. Non-wildcard searches still
    require a complete yyyy-mm-dd date.

Backend (demographiccontrol.jsp):

  • Removed the URLEncoder.encode(keyword) call so % (and characters like
    apostrophes) reach the query intact. The keyword is bound as a SQL parameter
    downstream, so encoding it was both unnecessary and corrupting. Wildcard
    detection is preserved.

How it works end-to-end

The live DOB query runs in demographicsearchresults.jsp via
DemographicDao.searchDemographicByDOB, which splits the keyword on - into
exactly three LIKE parameters (year/month/day). Padding to 2024-07-%
therefore produces year LIKE '2024%' AND month LIKE '07%' AND day LIKE '%%'
all patients born July 2024. Note: % is the wildcard (standard SQL LIKE); the
results page and the add-a-record page both <%@ include %> this same search
box, so the fix covers those screens too.

Testing

Verified manually:

  • Full dates (2024-07-15) — auto-hyphenation and submission still work.
  • Wildcard searches (2024-07-%, 2024-%) — return the expected patients.
  • Mid-field edits (20262025) — caret stays where it was, after the edit.

Summary by Sourcery

Relax auto-formatting of the patient DOB search field and preserve wildcard semantics while ensuring valid date input.

New Features:

  • Allow DOB searches to use '%' wildcards in the date field, automatically padding missing year/month/day segments for wildcard queries.

Bug Fixes:

  • Prevent aggressive date auto-formatting from disrupting mid-field edits, wildcard characters, and caret position in the DOB search field.
  • Stop URL-encoding search keywords so that wildcard characters and special characters (such as apostrophes) are preserved for downstream matching and SQL binding.

Enhancements:

  • Improve DOB search validation message to explain the supported YYYY-MM-DD format and wildcard usage.

Summary by Sourcery

Relax auto-formatting and validation of the patient DOB search field so wildcard searches work correctly and editing is not disruptive, while ensuring the backend preserves wildcard and special characters in search keywords.

New Features:

  • Support DOB wildcard searches in the UI by interpreting '%' in the date field and padding missing year/month/day segments before submission.

Bug Fixes:

  • Prevent the DOB search field auto-formatting from stripping '%' and forcing the caret to the end of the field during mid-text edits.
  • Stop URL-encoding search keywords in the demographic controller so wildcard and special characters reach downstream matching and SQL binding intact.

Enhancements:

  • Improve DOB search validation messaging to document the required YYYY-MM-DD format and how to use '%' as a wildcard.

@LiamStanziani LiamStanziani self-assigned this Jun 5, 2026
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 5, 2026

Reviewer's Guide

Relaxes the DOB auto-formatting logic in the patient search field to preserve wildcards and caret position, and stops URL-encoding the search keyword on the backend so wildcard and special characters are passed through intact for DOB searches.

Sequence diagram for wildcard DOB search flow

sequenceDiagram
    actor User
    participant Browser as Browser_DOB_field
    participant SearchBox as zdemographicfulltitlesearch_jsp
    participant Controller as demographiccontrol_jsp
    participant Dao as DemographicDao

    User->>Browser: Type DOB with % (e.g. 2024-07-% )
    Browser->>SearchBox: formatDateInput(input)
    Note right of SearchBox: Keep digits, '-' and '%';
    Note right of SearchBox: Auto-hyphenate only at end;
    Note right of SearchBox: Preserve caret position

    User->>Browser: Submit titlesearch form
    Browser->>SearchBox: checkTypeIn()
    alt dob contains %
        SearchBox->>SearchBox: Pad parts to 3 segments
        Note right of SearchBox: 2024-07 -> 2024-07-%
    else no % in dob
        SearchBox->>SearchBox: Validate full YYYYMMDD
    end

    Browser->>Controller: demographiccontrol_jsp with keyword
    Note right of Controller: Use keyword verbatim
    Note right of Controller: Do not URLEncoder.encode(keyword)

    Controller->>Controller: Detect wildcard with indexOf("%") or indexOf("*")
    Controller->>Dao: searchDemographicByDOB(keyword)
    Dao->>Dao: Split keyword on '-'
    Dao-->>Controller: DOB results
    Controller-->>Browser: Render demographicsearchresults_jsp
    Browser-->>User: Show patients matching wildcard DOB
Loading

File-Level Changes

Change Details Files
Adjust DOB input auto-formatting to preserve '%' wildcards and caret position while still guiding valid yyyy-mm-dd entry.
  • Replace naive digit-only stripping with filtering that preserves digits, hyphens, and '%' characters in the DOB field.
  • Limit automatic hyphen insertion to when the user is typing a plain date at the end of the field and no '%' wildcard is present.
  • Preserve a user-typed trailing hyphen so dates like '2024-07-' can be extended with a wildcard or day component.
  • Short-circuit formatting when the normalized output is unchanged from the previous value to avoid unnecessary caret manipulation.
  • Recalculate and restore the caret position based on non-hyphen characters so mid-field edits do not jump the cursor to the end.
src/main/webapp/demographic/zdemographicfulltitlesearch.jsp
Update DOB validation to support wildcard-based partial date searches while enforcing full dates for non-wildcard queries.
  • Detect '%' in the DOB field and treat it as a wildcard search trigger.
  • Pad wildcard queries with missing year/month/day segments so the backend receives exactly three '-' separated parts, defaulting missing parts to '%'.
  • Require a full 8-digit date (yyyy-mm-dd) only when no wildcard is present, rejecting incomplete non-wildcard dates.
  • Improve the validation alert message to document the YYYY-MM-DD format and '%' wildcard usage with an example.
src/main/webapp/demographic/zdemographicfulltitlesearch.jsp
Stop URL-encoding search keywords so wildcard and special characters are preserved for downstream matching.
  • Remove URLEncoder-related imports and exception handling from the demographic controller JSP.
  • Stop transforming the keyword via URLEncoder.encode so '%' and other special characters (e.g. apostrophes) are not corrupted.
  • Rely on existing downstream handling, where the keyword is bound as a SQL parameter and used for wildcard detection based on the presence of '%' or '*'.
  • Keep wildcard detection logic but operate on the raw keyword value when setting the 'regularexp' mode to 'like'.
src/main/webapp/demographic/demographiccontrol.jsp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@LiamStanziani LiamStanziani marked this pull request as ready for review June 5, 2026 19:31
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In formatDateInput, the caret logic assumes selectionStart is always available and the field is focused; consider guarding for null/undefined or non-focus cases to avoid runtime errors in older or nonstandard browsers.
  • The date sanitization regex in formatDateInput (/[^0-9%-]/g) allows multiple or leading hyphens and percent signs; if those are not intended, you may want to normalize or validate the structure (e.g., at most two hyphens and up to three segments) before reformatting.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `formatDateInput`, the caret logic assumes `selectionStart` is always available and the field is focused; consider guarding for `null`/`undefined` or non-focus cases to avoid runtime errors in older or nonstandard browsers.
- The date sanitization regex in `formatDateInput` (`/[^0-9%-]/g`) allows multiple or leading hyphens and percent signs; if those are not intended, you may want to normalize or validate the structure (e.g., at most two hyphens and up to three segments) before reformatting.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@LiamStanziani LiamStanziani requested a review from lacarmen June 5, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant