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

Notification

Icon
Error

How to implement custom UI property validations
xomega
#1 Posted : Friday, October 5, 2012 5:40:46 PM(UTC)
xomega



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.
  1. Declare a custom validation function that takes a DataProperty and a value as an object.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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).
  9. 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);
        }
    }
}
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.

Copyright © 2009-2022 Xomega.Net. All rights reserved.