Skip to content

[Blazor] Form mapping error handling and validation integration for SSR Blazor #48990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jun 26, 2023

Conversation

javiercn
Copy link
Member

@javiercn javiercn commented Jun 23, 2023

  • Implemented error handling and reporting for form data mapping for Minimal APIs and Blazor.

    • Minimal APIs: An exception is throw on the first validation error.
    • Blazor: A callback is provided to capture the errors. Binding continues when no exception
      is thrown from the callback. This enables us to capture as much information as possible in
      the presence of errors.
    • Errors are represented as FormattableString so that the error message can be localized if
      necessary by developers.
    • The error might contain the attempted value to allow redisplaying the correct value on the
      UI.
  • Adds support for reporting errors in Blazor. ModelBindingContext captures the errors and the
    attempted values during the binding process and makes them available to the application.

  • Updated the FormValueSupplier API to better integrate with model binding.

    • Removed the need for CanConvertSingleValue.
      • We now use the parameter name as part of the path to bind the parameter, for simple and
        complex values.
    • Updated CanBind to enable calling it without a form name when we just want to determine if
      a given type is bindable.
    • Updated the TryBind API to Bind and passed in a context object instead of multiple parameters.
  • Integrates with EditForm and EditContext:

    • Updated FieldIdentifier to hold the FieldPath when created from an expression.
    • Added a new ModelBindingValidator to report model binding errors in a form.

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jun 23, 2023
@javiercn javiercn changed the title [Blazor] Form validation for SSR Blazor [Blazor] Form mapping error handling and validation integration for SSR Blazor Jun 23, 2023
@SteveSandersonMS SteveSandersonMS self-requested a review June 26, 2023 11:17
Copy link
Member

@SteveSandersonMS SteveSandersonMS left a comment

Choose a reason for hiding this comment

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

Nice one! This looks like a solid implementation and it's great to see the extensive testing. I fully appreciate the design has to cope with multiple different viewpoints on how things work (SSR, interactive, MVC) so good job on balancing all that. Thanks for sorting out theFieldIdentifier-based representation of model errors too.

If we're switching the culture sensitivity by default then it would be ideal to do that in this preview release, but other than that, I'll back out and assume you'll resolve whichever remaining points make sense now and/or file follow-up issues.

@javiercn
Copy link
Member Author

/backport to release/8.0-preview6

@github-actions
Copy link
Contributor

Started backporting to release/8.0-preview6: https://github.com/dotnet/aspnetcore/actions/runs/5379221347

@javiercn javiercn merged commit cdfd8c0 into main Jun 26, 2023
@javiercn javiercn deleted the javiercn/form-validation branch June 26, 2023 18:43
@ghost ghost added this to the 8.0-preview7 milestone Jun 26, 2023
mkArtakMSFT pushed a commit that referenced this pull request Jun 26, 2023
…ation integration for SSR Blazor (#49031)

Backport of #48990 to release/8.0-preview6

/cc @javiercn

# [Blazor] Form mapping error handling and validation integration for SSR Blazor

Adds support for error handling and validation to Server Side Rendered Blazor.

## Description

Integrates the form data binding experience with Blazor and provides support for error handling.

Fixes #46983 (in this specific format)

## Customer Impact

This is one of the remaining major functionalities for Blazor Server Side rendering, so we want to make sure we can get as much feedback as possible on the experience. Specially since we are planning an app building exercise.

## Regression?

- [ ] Yes
- [X] No

[If yes, specify the version the behavior has regressed from]

## Risk

- [ ] High
- [ ] Medium
- [X] Low

The changes should be additive, and we've included a significant amount of test coverage for the new functionality.

## Verification

- [X] Manual (required)
- [X] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [X] N/A

----

## When servicing release/2.1

- [ ] Make necessary changes in eng/PatchConfig.props
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants