ValidationAttributeFormatterRegistry Class

Definition

Registry of IValidationAttributeFormatter factories keyed by ValidationAttribute type. Used by the validation localization pipeline to format localized error message templates with attribute-specific arguments.

public ref class ValidationAttributeFormatterRegistry sealed
public sealed class ValidationAttributeFormatterRegistry
type ValidationAttributeFormatterRegistry = class
Public NotInheritable Class ValidationAttributeFormatterRegistry
Inheritance
ValidationAttributeFormatterRegistry

Remarks

Resolution order:

  1. If the attribute implements IValidationAttributeFormatter itself (self-formatting), it is returned directly.
  2. If a factory is registered for the attribute's type via AddFormatter<TAttribute>(Func<TAttribute,IValidationAttributeFormatter>), it is used to create a formatter.
  3. Otherwise, null is returned, indicating that no attribute-specific formatter is available.

Built-in formatters for standard validation attributes (such as RangeAttribute, MinLengthAttribute, StringLengthAttribute, etc.) are registered automatically. Later registrations for the same attribute type replace earlier ones.

builder.Services.AddValidationLocalization(options =>
    options.AttributeFormatters.AddFormatter<CreditCardAttribute>(
        attribute => new CreditCardAttributeFormatter(attribute)));

Constructors

Name Description
ValidationAttributeFormatterRegistry()

Creates a new instance of ValidationAttributeFormatterRegistry with built-in formatters for standard validation attributes.

Methods

Name Description
AddFormatter<TAttribute>(Func<TAttribute,IValidationAttributeFormatter>)

Registers a formatter factory for the specified validation attribute type. Later registrations for the same type replace earlier ones.

GetFormatter(ValidationAttribute)

Returns an IValidationAttributeFormatter for the specified attribute. If the attribute implements IValidationAttributeFormatter itself, it is returned directly. Otherwise, the registry is consulted. Returns null if no formatter is registered for the attribute's type.

Applies to