Monday, 22 July 2013

Creating a model class with validation support using Prism for the Windows Runtime

The Windows Runtime has no in-built support for validation. Therefore in Prism for the Windows Runtime we added functionality to allow validation of user input and notification of validation errors back to the user. For more info see Validating user input in AdventureWorks Shopper.

In order to demonstrate the validation features of Prism I’ve decided to create a new small sample app. The app allows the user to enter their name, and then performs validation of the user input.

Implementation

The Microsoft.Practices.Prism.StoreApps library provides validation support to apps. In order to use Prism’s validation support your model classes must derive from the ValidatableBindableBase class. This class provides an error container whose contents are updated whenever a model class property value changes. The ValidatableBindableBase class derives from Prism’s BindableBase class, which raises property change notification events.

Specifying validation rules

Validation rules are specified by adding data annotation attributes that derive from the ValidationAttribute class to the declaration of the model property that you want to validate.

public class Person : ValidatableBindableBase
{
    private string _name;
 
    private const string RegexPattern = @"\A\p{L}+([\p{Zs}\-][\p{L}]+)*\z";
 
    [Required(ErrorMessage = "Name is required")]
    [RegularExpression(RegexPattern, ErrorMessage = "Name must contain only letters and spaces. Hypens are also allowed, but they cannot occur in sequences.")]
    public string Name
    {
        get { return _name; }
        set { SetProperty(ref _name, value); }
    }
}

In the Person class the Name property contains a Required attribute which specifies that a validation failure will occur if the field is null, contains an empty string, or contains only white-space characters. The RegularExpression attribute specifies that the Name property must match the regular expression given by the RegexPattern constant. This regular expression allows user input to consist of all unicode name characters as well as spaces and hyphens, as long as the spaces and hyphens don’t occur in sequences and are not leading or trailing characters. Each validation attribute also specifies the error message that will be returned to the user if validation fails.

Triggering validation when properties change

The SetProperty method in the ValidatableBindableBase class performs validation when a model property is set to a new value. The ValidatableBindableBase.SetProperty calls the BindableBase.SetProperty method, and then performs validation of the property value if it has changed. For more info about Prism’s internal implementation of this see Validating user input in AdventureWorks Shopper.

Summary

In this blog post I’ve created a model class with validation support. The Person class derives from Prism’s ValidatableBindableBase class, and has a property, Name, that possesses data annotation attributes that derive from the ValidationAttribute class to specify validation rules. When the value of the Name property changes validation is performed by calling the ValidatableBindableBase.SetProperty method.

In my next blog post I’ll hook up the model class with code that notifies the user of any validation errors.

No comments:

Post a comment