Telerik UI for Windows Phone by Progress

RadDataForm implements a flexible validation mechanism to make sure that input values fall into the expected ranges. The control provides a couple of validation mechanisms, as well as two validation modes which determine when the input in the fields will be validated against given rules.

Using the ValidationMode property

The ValidationMode property allows you to determine when the input in the editors will be validated. The following Validation Modes are currently supported:

  • OnValueChanged - input is validated when the value of the corresponding editor control is changed.
  • Manual - input is validated manually by calling the Commit method exposed by RadDataForm.

Using the ValidatingDataField event

One of the possible ways to validate input is to use the ValidatingDataField event exposed by RadDataForm. This event is fired each time a field is validated, i.e. its value is about to be set to the associated property on the data object. The event provides you with a reference to the validated field, a boolean property which defines whether the input is valid or not, and an error message which will be displayed in case the input is marked as invalid. The following code snippet demonstrates using the ValidatingDataField event:

CopyC#
private void radDataForm_ValidatingDataField(object sender, ValidatingDataFieldEventArgs e)
{
    if (e.AssociatedDataField.PropertyKey == "Password")
    {
        if (string.IsNullOrEmpty(e.AssociatedDataField.Value as string) || e.AssociatedDataField.Value.ToString().Length < 6)
        {
            e.IsInputValid = false;
            e.ValidationMessage = "Password must be at least 6 symbols long.";
        }
    }
}

Using Property Validators

Another way to implement input validation in RadDataForm is by using Validators. A Validator is a class that inherits from the DataFormValidator class and provides validation logic. Validators are defined for properties via the ValidatorAttribute class. It is possible to combine multiple validators for a single property. The following code snippet demonstrates how a validator for a password field can be implemented:

CopyC#
public class PasswordFieldValidator : DataFormValidator
{
    public override void Validate(ValidatingDataFieldEventArgs args)
    {
        if (args.AssociatedDataField.PropertyKey == "Password")
        {
            if (string.IsNullOrEmpty(args.AssociatedDataField.Value as string) || e.AssociatedDataField.Value.ToString().Length < 6)
            {
                args.IsInputValid = false;
                args.ValidationMessage = "Password must be at least 6 symbols long.";
            }
        }
    }
}

Once implemented, a Validator can be easily used by defining it on a property via the ValidatorAttribute:

CopyC#
[Validator(typeof(PasswordFieldValidator))]
public string Password
{
    get
    {
        return password;
    }
    set
    {
        password = value;
    }
}

Combining validators for a single property is done easily by enumerating all validator types in the ValidatorAttribute. In this case the validators will be executed in the order of their definition whereby the first failed validator will stop the validation logic.

The IsInputValid and InvalidFields properties

RadDataForm additionally exposes the IsInputValid and InvalidFields properties related to input validation. The IsInputValid property returns a booelan value determining whether there are currently any invalid fields in the form. The InvalidFields property returns an array containing the invalid fields, if any.

Validation Messages and the ValidationSummaryMode property

Once a the validation procedure for a field proves the input wrong, a corresponding validation message is displayed in RadDataForm according to the validation settings. Validation messages can be displayed below the corresponding data field or in above all fields at the top edge of RadDataForm. Displaying validation messages at both places is also possible. To determine where validation messages will be displayed, you should use the ValidationSummaryMode property which accepts the following values:

  • None - no validation messages will be displayed.
  • Form - validation messages will be displayed stacked at the top of the data form.
  • Field - validation messages will be displayed below the corresponding data field.

If you want to display validation messages both at the top of the form and below the data field you can combine the Form and Field values and set the combined value to the ValidationSummaryMode property.

Customizing Validation Messages

Validation messages can be easily customized by using the ErrorInfoMessage property exposed by DataField. When an Error Info Message is set the default message will be overriden in case the field is marked invalid.

Another way for customizing the error messages is to use the FieldInfoAttribute which also exposes the ErrorInfoMessage property.