Skip to content

Validation

BSick7 edited this page Jan 2, 2015 · 20 revisions

There are 3 methods of validation

Notes

ValidatesOnExceptions and ValidatesOnDataErrors can be used at the same time; however, exceptions will silence data error reporting.

All errors for a particular Control is maintained through an attached property Validation.Errors. This provides a source of error reporting.

FrameworkElement.BindingValidationError event is raised each time a validation error is added or removed. This can be used to customize error reporting different from Validation.Errors.

IDataErrorInfo

module Fayde.Data {
    export interface IDataErrorInfo {
        Error: string;
        GetError(propertyName: string): string;
    }
}

NOTE: This interface is incompatible with Silverlight. Fayde uses GetError instead of public string this[string propertyName] { ... }.

Error returns a single error message for the entire object; whereas, GetError allows the object to return an error message for a specific property. Typically, GetError is implemented using a Map. Use INotifyDataErrorInfo for more extensive error reporting.

Usage

To use, bind a control property to a model property whose owning object implements IDataErrorInfo. Then, set ValidatesOnDataErrors in the Binding to true.

View the testsite demo code:

INotifyDataErrorInfo

module Fayde.Data {
    export interface INotifyDataErrorInfo {
        ErrorsChanged: nullstone.Event<DataErrorsChangedEventArgs>;
        GetErrors(propertyName: string): nullstone.IEnumerable<any>;
        HasErrors: boolean;
    }
}

INotifyDataErrorInfo supports many errors per property as well as asynchronous error validation through the use of ErrorsChanged event.

ErrorsChanged event should raise any time an error is added or removed from backing error source. HasErrors should return false only if backing error source contains no errors. GetErrors should return all errors associated with propertyName from backing error source. Typically, the backing error source is Map<string, string[]>.

Usage

To use, bind a control property to a model property whose owning object implements INotifyDataErrorInfo. By default, ValidatesOnNotifyDataErrors in the Binding is set to true.

See the testsite demo for an example.

View the testsite demo code:

Exception-based

If a bound property setter throws an Exception or Error, a control can display the validation error from the exception/error thrown. The Exception.Message or Error.message will be used as the validation error message.

NOTE: Since this type of validation is strictly set-based, Control adorner will not display unless the validation happens from Binding --> Model updates.

Usage

To use, bind a control property to a model property which throws an Exception or Error when validation fails. Then, set ValidatesOnExceptions in the Binding to true.

View the testsite demo code:

Clone this wiki locally