Skip to content

fix(form-validation-message): clear messages when controls return to pristine - #1446

Open
iOvergaard wants to merge 4 commits into
mainfrom
v2/bugfix/clear-validation-messages-on-form-reset
Open

iOvergaard wants to merge 4 commits into
mainfrom
v2/bugfix/clear-validation-messages-on-form-reset

Conversation

@iOvergaard

@iOvergaard iOvergaard commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • uui-form-validation-message builds its message list from valid/invalid events, but a form control that is reset goes back to pristine and never reports its state again. Its validation message stayed on screen after form.reset().
  • formResetCallback in the form control mixin now dispatches a valid event, which the message element already handles. The message element has no relation to the pristine attribute.
  • Setting pristine = true on its own still reports nothing, as pinned by the existing mixin tests ("does not report Valid when going pristine again while still invalid").
  • After the reset, the mixin treats valid as the last reported state, so a control that is valid after the reset doesn't report it twice, and touching an invalid control reports invalid again.
  • Pre-existing behaviour since v1, found during a holistic review of UUIFormControlMixin.

How to test

  1. Create a uui-form wrapping a <form> with a required uui-input, a uui-form-validation-message, plus submit and reset buttons.
  2. Submit the empty form. The required message appears.
  3. Press reset. Before this fix the message stayed; now it clears.
  4. Submit again. The message comes back.

Test plan

  • Mixin: reports valid on reset even while invalid (fails without the fix), and reports invalid again when touched after a reset
  • form-validation-message: the message clears on form.reset() (fails without the fix)
  • Full suite: 1125 tests pass, tsc and lint clean

🤖 Generated with Claude Code

…pristine

Form controls only dispatch VALID/INVALID while not pristine, so a control
going back to pristine (form reset or programmatic) left its message on
screen forever. Since pristine is a reflected attribute, observe it on the
controls we currently show messages for and drop the message when the
control becomes pristine again.

Found during a holistic review of UUIFormControlMixin; pre-existing since v1.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 7, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a long-standing UX bug in uui-form-validation-message where validation messages could remain visible after a control returns to pristine (e.g., via form.reset()), by detecting pristine transitions and clearing the corresponding messages locally in the message component.

Changes:

  • Added a MutationObserver to watch pristine attribute changes on controls currently associated with displayed validation messages, removing messages when controls become pristine again.
  • Updated the VALID/INVALID event handlers to refresh the observed control set whenever the message map changes.
  • Added two tests covering message clearing on both form.reset() and programmatic pristine = true.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/components/form-validation-message/form-validation-message.element.ts Observe pristine attribute changes for currently-messaged controls and clear messages when they return to pristine.
src/components/form-validation-message/form-validation-message.test.ts Adds regression tests ensuring messages clear on reset and when a control is set back to pristine.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

@iOvergaard
iOvergaard requested a review from nielslyngsoe July 8, 2026 07:02
@iOvergaard

Copy link
Copy Markdown
Contributor Author

@nielslyngsoe this is trying to fix a latent bug where validation messages don't disappear if you "reset" the form. This works by observing the pristine attribute. Would you take a look and evaluate if this is something we ought to have in the UI library, please?

@iOvergaard iOvergaard added the bug Something isn't working label Jul 8, 2026
// back to pristine (e.g. on form reset) would leave its message behind. The `pristine`
// property is reflected, so we observe the attribute on controls we currently show
// messages for and remove them when they become pristine again.
private readonly _pristineObserver = new MutationObserver(entries => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be more right if a FormControl fired a valid event when it gets reset(when Pristine gets set again, so we avoid having any external relation to the pristine attribute)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks Niels, that's simpler 👍 I've reworked it in 8604429. formResetCallback now dispatches valid, and the message element doesn't look at pristine at all any more.

I've kept it to the reset rather than any pristine = true, because your tests from #1479 pin that going pristine on its own reports nothing ("does not report Valid when going pristine again while still invalid"). If you'd rather have it in the pristine setter, those two tests flip, and in the CMS UmbFormControlValidator.reset() would then mark a UUI control as valid the moment it resets. Happy either way.

I merged main in first, since the mixin has moved a lot since July. All 1125 tests pass.

iOvergaard and others added 2 commits November 20, 2026 11:15
…clear

A reset control is pristine, so it never reported its state again and
uui-form-validation-message kept showing the old message. formResetCallback
now dispatches a valid event, which the message element already handles,
instead of the message element observing the pristine attribute.

Setting pristine to true on its own still reports nothing, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-beach-055ecb503-1446.westeurope.azurestaticapps.net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants