IValidationAttributeFormatter Interface

Definition

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:

  1. Implement IValidationAttributeFormatter directly on the attribute itself. ValidationAttributeFormatterRegistry checks for this first and uses the attribute as its own formatter automatically.
  2. Create a separate IValidationAttributeFormatter implementation and register it via AttributeFormatters by calling AddFormatter<TAttribute>(Func<TAttribute,IValidationAttributeFormatter>).
The following example shows how to register a formatter for a custom attribute:
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 messageTemplate by substituting attribute-specific arguments alongside the displayName.

Applies to