Preserve modified state when InputBase parsing fails - #68365
Open
PureWeen wants to merge 3 commits into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0fb5ea3b-44bb-495b-99c5-93e753ca9ce1
Keep the mark-only EditContext operation internal and bridge it from InputBase without raising OnFieldChanged or expanding public API surface. Strengthen repeated-invalid, recovery, model-preservation, and browser coverage. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f88407e7-efcf-44b1-9387-cde2a2557973
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request adjusts Blazor Forms’ handling of InputBase parse failures so that invalid user input still marks the field as modified (preserving “dirty”/CSS behavior) but does not raise EditContext.OnFieldChanged, avoiding validators running against an unchanged model value (fixing #58407).
Changes:
- Refactors
EditContext.NotifyFieldChangedto delegate modified-state mutation to a new internalMarkAsModifiedhelper. - Updates
InputBase’s parse-failure path to mark the field modified without triggeringOnFieldChanged, while continuing to notify validation-state changes for parsing messages. - Adds unit and E2E coverage for the notification boundary (invalid → valid, valid → invalid, and empty input for nullable required scenarios).
Show a summary per file
| File | Description |
|---|---|
| src/Components/Forms/src/EditContext.cs | Introduces internal MarkAsModified and refactors NotifyFieldChanged to use it before raising OnFieldChanged. |
| src/Components/Web/src/Forms/EditContextAccessor.cs | Adds a narrow UnsafeAccessor bridge so Web can invoke EditContext.MarkAsModified without making it public. |
| src/Components/Web/src/Forms/InputBase.cs | Uses MarkAsModified (via accessor) on parse failure to avoid revalidating unchanged model values. |
| src/Components/Forms/test/EditContextTest.cs | Adds a unit test verifying MarkAsModified updates modified state without raising OnFieldChanged. |
| src/Components/Forms/test/EditContextDataAnnotationsExtensionsTest.cs | Adds a unit test verifying MarkAsModified does not trigger per-property validation. |
| src/Components/Web/test/Forms/InputBaseTest.cs | Expands/introduces unit tests asserting parse failures don’t raise OnFieldChanged and that subsequent valid input resumes normal behavior. |
| src/Components/test/testassets/BasicTestApp/FormsTest/TypicalValidationComponent.razor | Adds a required nullable number field to support the regression E2E scenario. |
| src/Components/test/E2ETest/Tests/FormsTest.cs | Adds E2E regression test ensuring parse failures don’t trigger model validation while preserving modified/invalid UI state. |
Review details
Tip
Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 8/8 changed files
- Comments generated: 0
- Review effort level: Lite
javiercn
reviewed
Aug 12, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f88407e7-efcf-44b1-9387-cde2a2557973
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
InputBaseparse failures without reporting a model value changeDescription
When
TryParseValueFromStringfails, the raw value in the input changed but the bound model value did not. The existing parse-failure path callsEditContext.NotifyFieldChanged, which does two things:OnFieldChanged, causing validators to run against the unchanged model value.That can produce both the parsing error and an unrelated model-validation error, such as
[Required], even thoughCurrentValue,ValueChanged, and the model were not updated.This change separates those two effects. A parse failure still marks the field modified, preserving the existing dirty-state and CSS behavior, but it no longer raises
OnFieldChanged. Parsing messages continue through the existingNotifyValidationStateChangedflow.Fixes #58407
Design
EditContext.NotifyFieldChangednow delegates its state mutation to a new publicMarkAsModifiedmethod before raisingOnFieldChanged.InputBasecalls onlyMarkAsModifiedwhen parsing fails.The API is symmetric with
MarkAsUnmodified(in FieldIdentifier)and gives custom input implementations a supported way to record user interaction without claiming that the bound model value changed. Forms and Web are separate assemblies, and framework code should not bridge that boundary withInternalsVisibleToorUnsafeAccessor, so the public API is the explicit contract between them.Areas I would especially like reviewed
modified invalidbehavior while suppressing only the value-change notification.EditContext.MarkAsModified(in FieldIdentifier)the right symmetric counterpart toMarkAsUnmodifiedfor this scenario?ValueChangedandOnFieldChangedpath exactly once.[Required]validation.Alternative PR
#65574 addresses the same issue by removing
NotifyFieldChangedfrom the parse-failure branch. That is much smaller, but it also changesEditContext.IsModified(field)fromtruetofalseafter invalid user input.This PR keeps the useful part of the existing behavior, namely modified-state tracking, while removing only the model-value-change notification. The main product decision between these PRs is whether failed input should remain modified, not whether the extra model validation should be suppressed.
Tests
InputBaseTest: 24/24 passed.EditContextTestandEditContextDataAnnotationsExtensionsTest: 43/43 passed.OnFieldChangedevent and passes with this change.