asp.net mvc - Manually trigger foolproof validation -


i'm using foolproof validation can use [requiredif] attributes on view model. problem i'd trigger validation within code using same logic outside controller.

i've tried creating own validation context , using validatior.tryvalidateobject; however, fails foolproof's custom requiredif validator. there way take model , validate other passing controller?

am using wrong approach?

here code:

var draftmodel = _drafthelper.loaddraft(draftid);  var validationresults = new list<validationresult>();  var vc = new validationcontext(draftmodel, null, null);  var isvaliddraft = validator.tryvalidateobject(draftmodel, vc,                                                 validationresults, true); 

and error on tryvalidateobject line

system.notimplementedexception: method or operation not implemented.

i think better approach use fluentvalidation, not foolproof validation. think fluent nicer attributes :).

using fluentvalidation, able validate model without use of controller.

draftvm draft = draftrepository.get(draftid);  var draftvalidator validator = new draftvmvalidator(); validationresult results = validator.validate(draft);  public class draftvmvalidator : abstractvalidator<draftviewmodel> {       public draftvmvalidator()       {           rulefor(vm => vm.optionalfield)             .must(befilledifnamenotempty)             .withmessage("optional field required because filled out name field");       }         public bool befilledifnamenotempty(draftvm viewmodel)       {             return !string.isnullorwhitespace(viewmodel.name);       }  }  

this not give system.notimplemented exception.

this validator dry because can plug asp.net mvc validation.
can call following code in global.asax or app_start etc. 1 validator all, bind mvc model validation or use in normal application.

fluentvalidationmodelvalidatorprovider.configure(); // bind 

if use inversion of control container ninject, fluentvalidation has plugin work that. more available on documentation in link provided above.

i have pretty big project example in github if want see more examples of validator instead of foolproof. example validators asp.net mvc 4


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -