.net - Declarative conditional validation of a field based on other one from parent model -


i have 2 separate types:

public class person {     public string name { get; set; }     public bool isactive { get; set; }      public contact contactdetails { get; set; } }  public class contact {     [requiredifactive]     public string email { get; set; } } 

what need perform conditional declarative validation of internal model field, based on state of parent model field - in particular example email has filled, if isactive option enabled.

i not want reorganize these models taxonomy, while in same time need use attribute-based approach. seems within attribute there no access validation context of parent model. how reach or inject there?

public class requiredifactiveattribute : validationattribute {     protected override validationresult isvalid(object value,                                                  validationcontext validationcontext)     {         /* validationcontext.objectinstance gives access current             contact type, there way of accessing person type? */ 

edit:

i know how conditional validation can implemented using fluent validation, i'm not asking (i don't need support regarding fluent validation). i'd know however, if exists way access parent model inside system.componentmodel.dataannotations.validationattribute.

my suggestion

go tools => library package manager => package manager console , install fluent validation.

enter image description here

action methods

[httpget] public actionresult index() {     var model = new person     {         name = "pkkg",         isactive = true,         contactdetails = new contact { email = "pkkg@stackoverflow.com" }     };     return view(model); } [httppost] public actionresult index(person p) {     return view(p); } 

fluent validation rules

public class mypersonmodelvalidator : abstractvalidator<person> {     public mypersonmodelvalidator()     {         rulefor(x => x.contactdetails.email)             .emailaddress()             .withmessage("please enter valid email address")             .notnull().when(i => i.isactive)             .withmessage("please enter email");     } } 

view models

[validator(typeof(mypersonmodelvalidator))] public class person {     [display(name = "name")]     public string name { get; set; }      [display(name = "isactive")]     public bool isactive { get; set; }      public contact contactdetails { get; set; } }  public class contact {     [display(name = "email")]     public string email { get; set; } } 

view

@{     var actionurl = url.action("action", "controller", new { area = "areaname" },                            request.url.scheme); } @using (html.beginform("action", "controller", formmethod.post,                                                      new { @action = actionurl }))     @html.editorfor(i => i.name);     @html.validationmessagefor(i => i.name);      @html.editorfor(i => i.isactive);     @html.validationmessagefor(i => i.isactive);      @html.editorfor(i => i.contactdetails.email);     @html.validationmessagefor(i => i.contactdetails.email);     <button type="submit">         ok</button> } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -