Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

How to implement custom UI data object validations
xomega
#1 Posted : Friday, October 5, 2012 6:10:34 PM(UTC)
xomega



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.
  1. Override the Validate method on your data object.
  2. Call the base.Validate method from the base class.
  3. Validate object's data properties or data objects contained in a child data object list.
  4. Add each validation error to the validationErrorList member of the object that is inherited from the base class by calling the AddError method.
  5. 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.
  6. 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);
        }
    }
}
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.