Skip to content

Fix Blazor client-side validation support for custom attributes - #69020

Merged
oroztocil merged 4 commits into
mainfrom
oroztocil/blazor-client-validation-init-fix
Sep 3, 2026
Merged

Fix Blazor client-side validation support for custom attributes#69020
oroztocil merged 4 commits into
mainfrom
oroztocil/blazor-client-validation-init-fix

Conversation

@oroztocil

@oroztocil oroztocil commented Sep 3, 2026

Copy link
Copy Markdown
Member

Fixes two problems in support for custom validation attributes in the Blazor SSR client-side validation feature.

1. JS validator registration timing: As an optimization, Blazor.formValidation was only created when the loaded page already contained the custom element carrying the rules, and only at DOMContentLoaded. So an app could not register custom validators from its startup code on a page with no validated form. For example, trying to register the custom validator in the continuation of Blazor.start().then(...) did not work and threw. This made use of custom validators difficult and unintuitive. After this fix, the Blazor.formValidation API is fully available immediately after Blazor.start runs.

2. Emitting rules for derived built-in attributes: An attribute deriving from a built-in one (for example RequiredIfAttribute : RequiredAttribute) had its IClientValidationRuleProvider rules silently discarded, because the built-in mapping matched the base type first and short-circuited. After this fix, custom rule providers take precedence over the built-in mapping.

Adds/updates tests to cover the new behavior.

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Sep 3, 2026
@oroztocil
oroztocil requested a balanced review from Copilot September 3, 2026 12:24

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.

🟢 Approval recommended

The reviewed changes are covered by targeted tests with no unresolved issues.

Pull request overview

Fixes Blazor SSR custom client-side validation registration and rule-provider precedence.

Changes:

  • Initializes Blazor.formValidation during startup.
  • Prioritizes custom rules over inherited built-in mappings.
  • Adds unit and E2E coverage.
File summaries
File Change
src/Components/Web.JS/test/Validation/Adapters/BlazorAdapter.test.ts Tests validator registration before forms exist.
src/Components/Web.JS/src/Validation/Adapters/BlazorAdapter.ts Creates the validation service unconditionally.
src/Components/Web.JS/src/Boot.Web.ts Initializes validation during startup.
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/ClientValidation/CustomValidator.razor.js Defines the test validator.
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Forms/ClientValidation/CustomValidator.razor Loads the validator module.
src/Components/test/E2ETest/ServerRenderingTests/ClientValidation/ClientValidationTest.cs Covers startup availability and custom validation.
src/Components/Endpoints/test/FormValidation/ClientValidationProviderTests.cs Tests custom-provider precedence.
src/Components/Endpoints/src/Forms/DataAnnotationsClientValidationProvider.cs Evaluates custom providers before built-in mappings.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@oroztocil
oroztocil marked this pull request as ready for review September 3, 2026 12:32
@oroztocil
oroztocil requested a review from a team as a code owner September 3, 2026 12:32
@oroztocil oroztocil added this to the 11.0-rc2 milestone Sep 3, 2026
@oroztocil
oroztocil merged commit afc567f into main Sep 3, 2026
31 checks passed
@oroztocil
oroztocil deleted the oroztocil/blazor-client-validation-init-fix branch September 3, 2026 14:49
@oroztocil

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

lewing pushed a commit that referenced this pull request Sep 4, 2026
…) (#69026)

* Fix Blazor client-side validation initialization to better support custom validator registration

* Try IClientValidationRuleProvider first in client validation rule generation to support derived built-in attributes

Co-authored-by: Ondřej Roztočil <roztocil@outlook.com>
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