Skip to content

Fix form binding error when child object has validation errors#66779

Open
sheiksyedm wants to merge 5 commits into
dotnet:mainfrom
sheiksyedm:fix/form-binding-child-object-error
Open

Fix form binding error when child object has validation errors#66779
sheiksyedm wants to merge 5 commits into
dotnet:mainfrom
sheiksyedm:fix/form-binding-child-object-error

Conversation

@sheiksyedm
Copy link
Copy Markdown

Description:

When submitting a form with child objects that have validation errors, the error handler lambda in SupplyParameterFromFormValueProvider.GetFormPostValue was incorrectly using parameterName (the root model name, e.g., "Model") instead of name (the actual field path, e.g., "Model.SubModels[0].SubModelNumber") as the error key.

This caused AttachParentValue to throw an InvalidOperationException because the stored error key didn't start with the parent prefix.

Root Cause

In SupplyParameterFromFormValueProvider.cs, the error handler for form-name-restricted forms was:

(name, message, value) => mappingContext.AddError(restrictToFormName, **parameterName**, message, value)

It should be:

(name, message, value) => mappingContext.AddError(restrictToFormName, **name**, message, value)

The name parameter in the lambda is the actual field path passed by the form data mapper, but parameterName (the root parameter name) was being used instead.

Testing

Added a unit test that exercises GetFormPostValue with a mock mapper simulating a child object binding error. The test fails without the fix and passes with it.

Fixes #65010

sheiksyedm and others added 2 commits May 21, 2026 20:45
When submitting a form with child objects that have validation errors,
the error handler was incorrectly using 'parameterName' (the root model
name) instead of 'name' (the actual field path) as the error key. This
caused AttachParentValue to fail because the error key didn't start with
the parent prefix.

Use the actual field name passed to the error handler so that the
parent-child relationship is preserved correctly.

Fixes dotnet#65010
Verifies that GetFormPostValue passes the actual field name (not the
root parameter name) to the error handler when a form has a restricted
form name. Without the fix, AttachParentValue throws because the error
key does not start with the parent prefix.
@sheiksyedm sheiksyedm requested a review from a team as a code owner May 21, 2026 17:02
Copilot AI review requested due to automatic review settings May 21, 2026 17:02
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label May 21, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 21, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Thanks for your PR, @sheiksyedm. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Blazor form post-binding error reporting for nested/child model validation failures when [SupplyParameterFromForm(FormName = ...)] is used, ensuring errors are keyed by the actual field path rather than the root parameter name.

Changes:

  • Update SupplyParameterFromFormValueProvider.GetFormPostValue to record mapping errors using the mapper-provided field key (name) when form-name restriction is enabled.
  • Add a unit test that simulates a child object binding error and verifies the error is stored under the correct key and can be associated with its parent container.

Reviewed changes

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

File Description
src/Components/Web/test/Forms/Mapping/FormMappingContextTest.cs Adds a regression test covering child-object error keying for form-name-restricted mapping.
src/Components/Web/src/Forms/Mapping/SupplyParameterFromFormValueProvider.cs Fixes the error handler to use the correct error key (name) when adding errors under a specific FormName.

Comment thread src/Components/Web/test/Forms/Mapping/FormMappingContextTest.cs Outdated
@sheiksyedm sheiksyedm force-pushed the fix/form-binding-child-object-error branch from 21f4c90 to a72c236 Compare May 21, 2026 17:07
Change 'does must start with' to 'must start with' in the
InvalidOperationException thrown by AttachParentValue.
@sheiksyedm sheiksyedm force-pushed the fix/form-binding-child-object-error branch from a72c236 to 51a9d96 Compare May 21, 2026 17:09
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with Blazor form binding when child object has validation error

2 participants