Skip to content

[release/11.0] Add message template formatting to DataAnnotations validation attributes - #132853

Open
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-132764-to-release/11.0
Open

[release/11.0] Add message template formatting to DataAnnotations validation attributes#132853
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-132764-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Backport of #132764 to release/11.0.

API proposal: #132605

/cc @jeffhandley @oroztocil

Customer Impact

  • Customer reported
  • Found internally

.NET 11 introduces first-class localization support in Microsoft.Extensions.Validation, consumed by Minimal APIs and Blazor. However, DataAnnotations does not expose a shared way for a caller to supply a localized message template while allowing each validation attribute to provide its own placeholder arguments.

Without this change, the .NET 11 localization implementation must retain generated per-attribute formatting switches, other hosts cannot reuse the same primitive, and duplicated formatting knowledge can diverge from the real attribute behavior. One existing example is FileExtensionsAttribute, where the attribute supplies normalized, dot-prefixed extensions while duplicated upper-layer formatters have supplied the raw property value.

The customer scenario has been requested repeatedly:

This backport adds the API-approved ValidationAttribute.FormatMessage primitive and preserves the attribute-specific formatting contract for built-in attributes and Options source-generated replicas.

We believe this meets the .NET 11 RC2 bug bar as:

  • Significant customer feedback on a feature new in the release.
  • A significantly incomplete message-presentation scenario in DataAnnotations that affects the new .NET 11 validation stack, including localization in MEV, Minimal APIs, and Blazor.

Regression

  • Yes
  • No

This is not a regression from .NET 10. The API and the first-class Microsoft.Extensions.Validation localization scenario are new in .NET 11.

Testing

On main:

  • System.ComponentModel.Annotations built successfully.
  • System.ComponentModel.Annotations.Tests: 1,008 passed, 0 failed.
  • Microsoft.Extensions.Options built successfully.
  • Microsoft.Extensions.Options.Tests: 177 net11 tests and 158 net481 tests passed.
  • Microsoft.Extensions.Options.SourceGeneration.Unit.Tests: 103 net11 tests and 82 net481 tests passed, with one expected conditional skip.
  • Microsoft.Extensions.Options.SourceGeneration.Tests: 68 net11 tests and 68 net481 tests passed.
  • Generated replicas were compiled and executed for C# 10 and C# 11.
  • The .NET Framework generated output remains unchanged.

The same component tests and release-branch CI will validate the backport on release/11.0.

Risk

Low-to-moderate.

The change adds an API-reviewed public virtual method and explicit overrides on the built-in attributes that require additional format arguments. It is additive and removes no existing surface.

Existing FormatErrorMessage behavior, argument ordering, and CurrentCulture formatting remain unchanged. The Options source generator emits matching overrides only when the target compilation exposes the new API, leaving down-level and .NET Framework output unchanged.

The main implementation risks are public virtual dispatch and source-generator parity. These are mitigated by:

No package references, dependencies, or package-authoring project settings change.

Note

This pull request description was generated with GitHub Copilot.

…tes (#132764)

## Summary

Adds the approved `ValidationAttribute.FormatMessage` API so callers can
supply externally localized composite-format strings while each
validation attribute remains responsible for providing its placeholder
arguments.

## Changes

- Add
`ValidationAttribute.FormatMessage([StringSyntax("CompositeFormat")]
string format, string name)`.
- Route the base `FormatErrorMessage` implementation through the new
method.
- Add overrides for `CompareAttribute`, `FileExtensionsAttribute`,
`LengthAttribute`, `MaxLengthAttribute`, `MinLengthAttribute`,
`RangeAttribute`, `RegularExpressionAttribute`, and
`StringLengthAttribute`.
- Preserve existing placeholder ordering and `CurrentCulture`
formatting.
- Add coverage for supplied formats, virtual dispatch, culture-sensitive
formatting, invalid formats, and null arguments.

Existing `FormatErrorMessage` behavior remains unchanged.

## Testing

- `dotnet build` — succeeded with 0 warnings and 0 errors.
- `dotnet build /t:test
.\tests\System.ComponentModel.Annotations.Tests.csproj` — 1,008 passed,
0 failed, 0 skipped.

Fixes #132605

> [!NOTE]
> This pull request description was generated with GitHub Copilot.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 524df8bb-0555-4425-b73f-bc8340d0dfc2
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.

@ViveliDuCh
ViveliDuCh requested a review from oroztocil August 27, 2026 21:27

@artl93 artl93 left a comment

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.

completing a new feature. approved.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants