How It Works ...

WebFu validation can be run under virtually any context. This includes Web Forms, AJAX, web services, Windows Forms, NT services, console applications and MVC pages.

At its core, WebFu validation requires two things:

1. A set of validation rules ...

These are provided by any implementation of an IWFRuleProvider. The built-in providers include:
2. A collection of values to validate against ...
WebFu matches up rules against values to validate. Values are provided by any implementation of IWFValueProvider. The built-in providers include:
An instance of a 'model' is not strictly necessary, but is usually included when validation is performed.

How To Manually validate a model using DataAnnotations

You can validate a model using WFUtilities.TryValidateModel. Here is an example to validate the model against DataAnnotations on its own properties:
//Declare a metadata object. This will store any validation errors.
//An HtmlHelper object also has its own WFModelMetaData object,
//which will automatically apply validation error CSS classes if validation has failed prior to rendering.
WFModelMetaData metaData = new WFModelMetaData();

//We need an IWFRuleProvider which provides the validation rules.
//We can use the built-in WFTypeRuleProvider that provides validation rules from any class type or object.
WFTypeRuleProvider ruleProvider = new WFTypeRuleProvider(modelObject);

//We need an IWFValueProvider which provides the values to validate against.
//For example, we might want to validate against form data, an object, or a dictionary of values.
//Here, we'll just validate against the model object itself:
WFObjectValueProvider valueProvider = new WFObjectValueProvider(modelObject, "");

WFUtilities.TryValidateModel(modelObject, "", valueProvider, metaData, ruleProvider);


If you wish to use a special set of rules to validate against, you can specify a type when declaring the WFTypeRuleProvider object. IE:
User modelObject = new User(); //The model
Type userProxyValidationType = typeof(UserValidationProxy); //The type that has the DataAnnotations

WFTypeRuleProvider ruleProvider = new WFTypeRuleProvider(userProxyValidationType);

WFUtilities.TryValidateModel(modelObject, "", valueProvider, metaData, ruleProvider);

Last edited May 18, 2012 at 11:31 PM by aikeru, version 1

Comments

No comments yet.