开发者

How to set up FubuMVC validation

I'm trying to learn FubuMVC and have gotten stuck on validating my input models. What I want to accomplish is post-validate-redi开发者_StackOverflowrect. That is, to redirect to same view and show the errors if the model is invalid. I'm using attributes on my models.

Also, how would I specify my own error messages, i.e localization?

I'm using the latest packages of Fubu from nuget.

My registry looks like this:

IncludeDiagnostics(true);
Applies.ToThisAssembly();

Actions.IncludeClassesSuffixedWithController();

Routes
    .HomeIs<HomeController>(x => x.Index())
    .IgnoreControllerNamesEntirely()
    .IgnoreMethodsNamed("Index")
    .IgnoreMethodsNamed("Query")
    .IgnoreMethodsNamed("Command")
    .IgnoreNamespaceText("Features")
    .IgnoreMethodSuffix("Html")
    .RootAtAssemblyNamespace()
    .ConstrainToHttpMethod(x => x.Method.Name.EndsWith("Command"), "POST")
    .ConstrainToHttpMethod(x => x.Method.Name.EndsWith("Query"), "GET");

this.UseSpark();
this.Validation();

HtmlConvention<SampleHtmlConventions>();
Views.TryToAttachWithDefaultConventions();


The FubuMVC.Validation package is really just an example of how to use FubuValidation as we haven't built it out for all of the edge cases. Having said that, let me explain a little bit about how it works so we can see if you can use it, or if you should just handroll your own validation behavior.

The ValidationBehavior uses the IValidationFailureHandler interface to "handle" validation failures. The Notification object built up from FubuValidation is shoved into the IFubuRequest when the behavior fires, and then the handler is called.

The ValidationFailureHandler class is wired up by default for all validation failures. This delegates to the IValidationFailurePolicy to determine the strategy to use for a given model type (see my post on policies for an explanation of how this works).

The Validation extension method has an overload which gives a micro- dsl for configuring these policies:

this.Validation(x => { 
    x.Failures.... 
}); 

From here you can 1) apply custom policies via the ApplyPolicy method or 2) use the predicate based configuration approach via the IfModel methods.

If you go the predicate route (e.g., x.Failures.IfModelIs()), you can tell FubuMVC.Validation to use FubuContinuations to redirect or transfer to another behavior chain. Rex recently posted about FubuContinuations if you're looking for some guidance in this area (http://rexflex.net/2011/07/fubumvc-fubucontinuation/).

Hope this helps and feel free to ask away if I didn't explain anything enough,
Josh

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜