validation - How do I custom validate collections withing MVC5 -
i building mvc5 application , have following viewmodels:
public class userpartyviewmodel { public userpartyviewmodel() { entitlements = new collection<assignedclaims>(); } public guid partyid { get; set; } public string partyname { get; set; } public icollection<assignedclaim> assignedclaims{ get; set; } } public class assignedclaims { public assignedclaims() { claimvalues = new collection<assignedclaimvalue>(); } public string name { get; set; } public int max { get; set; } public int min { get; set; } public icollection<assignedclaimvalue> claimvalues { get; set; } } public class assignedclaimvalue { public guid claimvalueid { get; set; } public string claimvalue { get; set; } public bool assigned { get; set; } }
contained in userpartyviewmodel assignedclaim name of "security" , assignedclaimvalue claimvalue of "user"
if claimvalue of user assigned need validate rest of model. if not no further validation should take place.
within assignedclaims there min , max, these minimum , maximum number of assignedclaimvalues should assigned.
i have tried use attributevalidate cannot stop validating rest of model.
i have looked @ ivalidatableobject interface can't work out how control validation of child collections depending on user claim.
what's best way achieve this?
found solution appears want:
public class userpartyviewmodel : ivalidatableobject { public userpartyviewmodel() { entitlements = new collection<assignedclaims>(); } public string accesslevel { get; set; } public guid partyid { get; set; } public string partyname { get; set; } public icollection<assignedclaims> entitlements { get; set; } public ienumerable<validationresult> validate(validationcontext validationcontext) { var isuser = entitlements.any(c => c.name == "security" && c.claimvalues.any(v => v.assigned == true && v.claimvalue == "user")); if (isuser) { int = 0; foreach (var result in entitlements) { yield return result.validate(i++); } } else { yield return validationresult.success; } } } public class assignedclaims { public assignedclaims() { claimvalues = new collection<assignedclaimvalue>(); } public string name { get; set; } public string description { get; set; } public int max { get; set; } public int min { get; set; } public icollection<assignedclaimvalue> claimvalues { get; set; } public validationresult validate(int item) { int min = min; int max = (claimvalues.count() < max) ? claimvalues.count() : max; int assignedcount = claimvalues.where(i => i.assigned == true).count(); if (!(min <= assignedcount && assignedcount <= max)) { string errmessage = string.format("{2} should have between {0} , {1} security claims checked.", min, max, name); return new validationresult(errmessage, new[] { string.format("entitlements[{0}]", item) }); } else { return validationresult.success; } } }
the issue had trying error messages appearing in correct place. in view assignedclaims added:
@html.validationmessagefor(model => model, "", new { @class = "text-danger" })
and passed iteration through validate function on assignedclaim ensure added correct member.
Comments
Post a Comment