Because ValidationAttributes are metadata, their objects cannot be modified when retrieved from properties.
Sometimes you need to create or customize ValidationAttributes at runtime, such as comparing a date field to today’s
date. Implement the PeterBlum.DES.DataAnnotations.ICustomizeDataField interface on your Entity class.
This interface defines a method that is passed a PeterBlum.DES.DataAnnotations.ActiveDataField object.
Its properties define business rules about the DataField that you can modify, including validators.
In this example, it will be used to establish a DESDA.RangeAttribute on the Employee.BirthDate field
so that dates are between 100 and 18 years from today's date. The call to activeDataField.GetEditableValidationAttribute()
actually modifies activeDataField so its list of ValidationAttributes includes the desired
ValidationAttribute.
public partial class Employee : DESDA.ICustomizeDataField
{
public void CustomizeDataField(DESDA.ActiveDataField activeDataField)
{
switch (activeDataField.DataField)
{
case "BirthDate":
DESDA.RangeAttribute vRangeAttribute =
activeDataField.GetEditableValidationAttribute<DESDA.RangeAttribute>(true);
vRangeAttribute.Minimum = DateTime.Today.AddYears(-100);
vRangeAttribute.Maximum = DateTime.Today.AddYears(-18);
break;
}
}
}
public class EmployeeMetadata
{
[DESDA.DateDataType()]
[DESDA.Required()]
public object BirthDate { get; set; }
}
The code files which implement ICustomizeDataField are shown in the Source Code Browser.
In the next topic, you'll learn about adding custom
validation code.
Open the Source Code Browser (C# only)