Class TlumachStringLengthAttribute
- Namespace
- Tlumach.DataAnnotations
- Assembly
- Tlumach.DataAnnotations.dll
A StringLengthAttribute that takes its error message from a class created by Tlumach Generator.
The message template receives the display name of the member as {0}, MaximumLength as {1} and
MinimumLength as {2}, which is the order that StringLengthAttribute itself uses.
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class TlumachStringLengthAttribute : StringLengthAttribute, ITlumachLocalizedErrorMessage
- Inheritance
-
TlumachStringLengthAttribute
- Implements
- Inherited Members
Examples
With passwordLength translated as The {0} must be at least {2} and at max {1} characters long.:
[TlumachStringLength(100, typeof(Strings), Strings.passwordLengthKey, MinimumLength = 6)]
public string? Password { get; set; }
Constructors
TlumachStringLengthAttribute(int)
Initializes a new instance of the TlumachStringLengthAttribute class. Set TranslationClass and TranslationKey in the annotation, or leave both unset to behave exactly like StringLengthAttribute.
public TlumachStringLengthAttribute(int maximumLength)
Parameters
maximumLengthintThe maximum allowed length of the string.
TlumachStringLengthAttribute(int, Type, string)
Initializes a new instance of the TlumachStringLengthAttribute class that takes its message from the given translation.
public TlumachStringLengthAttribute(int maximumLength, Type translationClass, string translationKey)
Parameters
maximumLengthintThe maximum allowed length of the string.
translationClassTypeThe class created by Tlumach Generator, or a nested group class inside one, that provides the message.
translationKeystringThe name of the translation unit member in
translationClass, or the key of a translation entry.
Properties
CultureSource
Gets the culture, for which the message is read.
public TranslationCultureSource CultureSource { get; set; }
Property Value
TranslationClass
Gets the class created by Tlumach Generator, or a nested group class inside one, that provides the message.
public Type? TranslationClass { get; set; }
Property Value
TranslationKey
Gets the name of the translation unit member in TranslationClass, or the key of a translation entry.
public string? TranslationKey { get; set; }
Property Value
Methods
FormatErrorMessage(string)
Formats the error message, taking it from the configured translation when there is one.
public override string FormatErrorMessage(string name)
Parameters
namestringThe display name of the member being validated. It becomes the
{0}argument of the template.
Returns
- string
The formatted message.
Exceptions
- InvalidOperationException
Thrown when the lengths of the attribute are illegal, when the Tlumach configuration of the attribute is invalid, or when the message cannot be resolved.