Skip to content

Preserve modified state when InputBase parsing fails - #68365

Open
PureWeen wants to merge 4 commits into
dotnet:mainfrom
PureWeen:pureween-fix-inputbase-parse-notification
Open

PureWeen wants to merge 4 commits into
dotnet:mainfrom
PureWeen:pureween-fix-inputbase-parse-notification

Conversation

@PureWeen

@PureWeen PureWeen commented Aug 11, 2026 •

Copy link
Copy Markdown
Member

Fix InputBase parse failures without reporting a model value change

Description

When TryParseValueFromString fails, the raw value in the input changed but the bound model value did not. The existing parse-failure path calls EditContext.NotifyFieldChanged, which does two things:

  1. Marks the field modified.
  2. Raises 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 though CurrentValue, 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 existing NotifyValidationStateChanged flow.

Fixes #58407

Design

EditContext.NotifyFieldChanged now delegates its state mutation to a new public MarkAsModified method before raising OnFieldChanged. InputBase calls only MarkAsModified when 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 with InternalsVisibleTo or UnsafeAccessor, so the public API is the explicit contract between them.

Areas I would especially like reviewed

  1. Modified-state contract: Is a user-entered but unparseable value still considered modified? This PR preserves the existing modified invalid behavior while suppressing only the value-change notification.
  2. Public API shape: Is EditContext.MarkAsModified(in FieldIdentifier) the right symmetric counterpart to MarkAsUnmodified for this scenario?
  3. Notification boundary: The first valid value after a failed parse must resume the normal ValueChanged and OnFieldChanged path exactly once.
  4. Opposite boundary: Empty input for nullable values remains a successful model change and still runs normal [Required] validation.

Alternative PR

#65574 addresses the same issue by removing NotifyFieldChanged from the parse-failure branch. That is much smaller, but it also changes EditContext.IsModified(field) from true to false after 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.
  • EditContextTest and EditContextDataAnnotationsExtensionsTest: 43/43 passed.
  • Browser E2E: Server and WebAssembly cases passed 2/2.
  • The regression assertion fails against the pre-fix source with one unexpected OnFieldChanged event and passes with this change.
  • Published Release-trimmed WebAssembly and Release-trimmed WebAssembly AOT both passed the initial-invalid, valid, valid-to-invalid, and empty-input browser transitions with no console, page, or network failures.
  • After the public API rework, the complete Forms and Web test projects build and pass with zero warnings or errors.

Copilot AI added 2 commits August 11, 2026 15:01
Co-authored-by: Copilot App <[email protected]>

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 <[email protected]>

Copilot-Session: f88407e7-efcf-44b1-9387-cde2a2557973
Copilot AI lite review requested due to automatic review settings August 11, 2026 20:07
@PureWeen
PureWeen requested a review from a team as a code owner August 11, 2026 20:07

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

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.NotifyFieldChanged to delegate modified-state mutation to a new internal MarkAsModified helper.
  • Updates InputBase’s parse-failure path to mark the field modified without triggering OnFieldChanged, 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

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: f88407e7-efcf-44b1-9387-cde2a2557973
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

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.

Copilot review overview

🟢 Approval recommended

The implementation preserves existing dirty-state semantics while cleanly separating model-change notifications, with comprehensive unit and browser coverage.

Review effort: Balanced
Findings: None

@PureWeen PureWeen left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Written with GitHub Copilot (AI), with human direction. Posted by PureWeen.

EditContext.NotifyFieldChanged(FieldIdentifier);
// The raw input changed, but the model value did not. Track the user interaction
// without revalidating the unchanged model value.
EditContext.MarkAsModified(FieldIdentifier);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can we also notify the field when parsing successfully recovers to the same model value? For a [Required] int? that starts null, the sequence null -> "1e+10" -> empty clears the parsing message, but assigning null hits CurrentValue's equality gate and never raises NotifyFieldChanged. The model remains null with no Required message, and the browser renders the field as modified valid.

The current E2E path enters 5 before the final empty value, so it only covers a value-changing recovery. Please add direct null -> invalid -> empty coverage and notify exactly once on successful recovery, while continuing to avoid model validation on the parse failure itself.

@PureWeen

Copy link
Copy Markdown
Member Author

/review

Restore field validation after successful parsing even when the model value is unchanged, without invoking ValueChanged or duplicating changed-value notifications. Cover nullable Required and same-value Range recovery through real input binders and Server/WebAssembly browser tests.

Co-authored-by: Copilot App <[email protected]>
@PureWeen

Copy link
Copy Markdown
Member Author

/review

This review was created by GitHub Copilot with help from a human.

@kubaflo kubaflo 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.

Current-head review — e36af28fd03dfa96284aee0739ab1b9bd7c65432

No new high-confidence actionable finding identified in the changed parsing/notification state machine. I traced parse failure, repeated failure, value-changing recovery, same-value recovery, and nullable-empty recovery against CurrentValue's equality gate and the new managed/browser assertions. Failure retains modified state without OnFieldChanged; successful same-value recovery has an explicit notification, while changed values use the existing notification path. The previous failure flag is captured before recovery callbacks.

Prior feedback: The cross-assembly UnsafeAccessor/IvT objection is addressed by the public API/baseline entry; no accessor bridge remains in this diff. The null → invalid → empty recovery finding is addressed in source by the same-value notification and explicit nullable theory row, plus browser coverage. I am not claiming independent green execution or resolving that thread. The alternative #65574 is already disclosed in the description and makes a different modified-state tradeoff, so it is not reported as a new duplicate defect.

Validation and limits: Frozen git diff --check passes. Inspected full changed files, InputBase's surrounding setter, shared form fixture/model constraints, and relevant existing form-test callers. No local Forms/Web build, browser run, or mutation replay was performed; the author's reported passes/AOT results are not independently certified here. The exact head has failed main CI, Helix monitor, CoreCLR/Mono-server E2E, and Build Analysis/Insights checks, alongside successful platform/template/Mono-client-WebAssembly checks. Check summaries alone do not establish cause; raw Azure logs were not analyzed, so failures remain unresolved validation evidence rather than attributed findings.

Scope: GPT-6.1 Sol; no model campaign or visual/browser validation. Public API approval/release readiness was not established; COMMENT only.

/// Marks the specified field as modified.
/// </summary>
/// <param name="fieldIdentifier">Identifies the field whose modification flag should be set.</param>
public void MarkAsModified(in FieldIdentifier fieldIdentifier)

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.

Why this helper method is needed?

@kubaflo kubaflo 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.

Feedback follow-up: the helper has a concrete caller; public API design remains a human decision. No new code revision or new actionable correctness finding.

Revisited the question about why MarkAsModified is needed against the full EditContext/InputBase source at the unchanged head, the PR diff, and all three feedback surfaces.

The method separates two effects currently combined by NotifyFieldChanged: setting the field's modification flag and invoking OnFieldChanged. InputBase.CurrentValueAsString calls only MarkAsModified when parsing fails, so invalid user input keeps dirty/CSS state without notifying validators that the unchanged model value changed. Normal successful changes still call NotifyFieldChanged; successful recovery to the same value has its own explicit notification path. Calling NotifyFieldChanged instead would reintroduce the behavior this PR removes, while removing the parse-failure call altogether would also remove modified-state tracking.

That explains the operation, not automatic acceptance of the proposed public API. The earlier no cross-assembly IvT/UnsafeAccessor feedback explains why this version exposes an operation rather than using a private bridge. Whether EditContext.MarkAsModified(in FieldIdentifier) is the desired public shape, or whether the behavior should instead change, still needs maintainer/API judgment. The existing description already calls out that tradeoff and the alternative implementation.

The previous same-value-recovery finding remains addressed in the inspected source; this comment does not resolve anyone's thread. No tests were rerun for the new question, no browser/visual validation was performed, and author-reported runtime results remain unverified here. Required Build Analysis still reports failure; this feedback-only pass does not classify the CI cause or certify merge readiness.

Direct GPT-6.1 Sol source/feedback reconciliation. Overall safety confidence remains low, matching the prior review's execution/CI limitations. COMMENT only.

This branch has not been deployed

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

Labels

area-blazor Includes: Blazor, Razor Components pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] InputBase - failed parsing (eg. InputNumber) triggers field validation

7 participants