Many of the UI side validations in Xomega Framework can be covered by single property validations that either come standard with the framework, or
custom property validations that you implement.
However, sometimes you may need to implement cross-field validation that is based on more than one property, such as that the start of the range does not exceed the end of the range, or cross-object validation in an object list, such as checking that values of a certain property are unique for all the objects in the list. This kind of validation would not be typically triggered when the user edits individual fields, but rather prior to submitting the values in the form.
To implement custom object level validation, you can use the following steps.
- Override the Validate method on your data object.
- Call the base.Validate method from the base class.
- Validate object's data properties or data objects contained in a child data object list.
- Add each validation error to the validationErrorList member of the object that is inherited from the base class by calling the AddError method.
- It is generally recommended to include the actual values and mention the field names in the error messages. This will help the user to identify and fix the errors when there are multiple validation errors.
- You can use numbered placeholders, such as {0}, for parameters in the error message followed by a list of parameters' values in the AddError method.
The following example illustrates implementation of a custom object validation.
Code:
public partial class EmployeeObject
{
public override void Validate(bool force)
{
base.Validate(force);
// cross-field validation
if (HireDateProperty.Value != null && VacationHoursProperty.Value != null &&
HireDateProperty.Value.Value > DateTime.Today.AddYears(-1) &&
VacationHoursProperty.Value.Value > 100)
{
validationErrorList.AddError(
"Invalid vacation hours {0} for a hiring date {1}. " +
"Up to 100 vacation hours allowed in the first year.",
VacationHoursProperty.Value, HireDateProperty.Value);
}
}
}