|
Xomega Framework comes with a number of base or common UI data properties, which support many standard validations out of the box. Yet the framework also provides great flexibility for implementing custom data property validations on the UI side. To implement a custom validation on the property level you can follow the steps below. - Declare a custom validation function that takes a DataProperty and a value as an object.
- Implement the validation of the value in that function. You can use specific attributes of the data property that is passed in. For example, validations for text properties may use the size attribute of the property to validate the length of the text represented by the value.
- If the value is invalid, add an error to the ValidationErrors member of the data property. When building the error message, you can use the string representation of the data property to output the current label or name of the property to indicate which field failed the validation in case when multiple validation errors are displayed to the user.
- For multi-valued properties, this validation function will be called for each individual value. Therefore, it is generally a good idea to include the actual value into the error message as well, so that the user would know which specific value failed the validation.
- 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 that you call when adding validation errors.
- After you define the validation function, you can assign or add it to the Validator delegate of a specific data property. Assigning it will replace all current validations for that property, while adding it will execute the custom validation in addition to all other validations.
- If your validation is common to a certain type of properties, then you may want to consider creating a custom property, defining the validation function on that property and adding it to the Validator in the property constructor.
- If you want to customize validation of a property on a specific data object, then you can add the validation function to the property's Validator during the object's initialization (Initialize method for manually created objects and OnInitialized method for objects that extend generated objects).
- You can also add an anonymous validation function as a delegate instead of declaring this function as in the first step.
The following example demonstrates the above steps. Code:
public partial class EmployeeObject
{
protected override void OnInitialized()
{
LoginProperty.Validator += ValidateLogin;
NationalIDNumberProperty.Validator += delegate(DataProperty dp, object val)
{
// perform object-specific property validation
};
}
// static login property validator
public static void ValidateLogin(DataProperty dp, object value)
{
if (dp != null && !Convert.ToString(value).Contains("\\"))
{
dp.ValidationErrors.AddError(
"{0} should be prefixed by domain name. Invalid value: {1}.", dp, value);
}
}
}
|