Fix form binding error when child object has validation errors#66779
Open
sheiksyedm wants to merge 5 commits into
Open
Fix form binding error when child object has validation errors#66779sheiksyedm wants to merge 5 commits into
sheiksyedm wants to merge 5 commits into
Conversation
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.
Contributor
|
Thanks for your PR, @sheiksyedm. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Contributor
There was a problem hiding this comment.
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.GetFormPostValueto 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. |
21f4c90 to
a72c236
Compare
Change 'does must start with' to 'must start with' in the InvalidOperationException thrown by AttachParentValue.
a72c236 to
51a9d96
Compare
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.
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