IValidationAttributeFormatter Interface
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Formats a validation error message template with attribute-specific arguments.
Used by the validation localization pipeline to produce fully formatted error messages
from localized templates that contain positional placeholders beyond {0} (the display name).
public interface class IValidationAttributeFormatter
public interface IValidationAttributeFormatter
type IValidationAttributeFormatter = interface
Public Interface IValidationAttributeFormatter
Remarks
To add formatting support for a custom validation attribute, you have two options:
- Implement IValidationAttributeFormatter directly on the attribute itself. ValidationAttributeFormatterRegistry checks for this first and uses the attribute as its own formatter automatically.
- Create a separate IValidationAttributeFormatter implementation and register it via AttributeFormatters by calling AddFormatter<TAttribute>(Func<TAttribute,IValidationAttributeFormatter>).
public class MyAttributeFormatter(MyAttribute attribute) : IValidationAttributeFormatter
{
public string FormatErrorMessage(CultureInfo culture, string messageTemplate, string displayName)
=> string.Format(culture, messageTemplate, displayName, attribute.CustomProperty);
}
// Register it in Program.cs:
builder.Services.AddValidationLocalization(options =>
options.AttributeFormatters.AddFormatter<MyAttribute>(
attribute => new MyAttributeFormatter(attribute)));
Methods
| Name | Description |
|---|---|
| FormatErrorMessage(CultureInfo, String, String) |
Formats the specified |