c# 4.0 - Custom Validation For Required fields -
c# 4.0 - Custom Validation For Required fields -
hi want have validations of sort
[requiredcustom(actiontype=(int)action.update, actiontype=(int)action.delete)] public string notesid { get; set; } [required] [regularexpression("1|2|3|4")] public int actiontype { get; set; }
i want validate notesid when updation , deletion taking place. don't need javascript code unobtrusive , all. want server side validation.
please don't suggest utilize of separate models can't that. similar solution do.
let me know if requires more clarification, help appreciated.
if ok placing validation info annotations on class itself, reply post (by gary.s) suggests.
here
in essense, create new custom validation attribute , since attribute @ class level, have access object , hence access other properties.
you custom attribute should (assuming class name 'notes')
public class requiredcustom: validationattribute { list<int> actions; public requiredcustom(int[] actions) { this.actions = new list<int>(); this.actions.addrange(actions); } public override bool isvalid(object value) { bool isvalid = true; notes testval = value notes; if(this.actions.contains((int)testval.action)) { if(string.isnullorwhitespace(testval.notesid)) { isvalid = false; } } homecoming isvalid; } }
then, add together validation attribute on class , send array of enums want test for. way dynamic plenty send other later. in case, sending array of update , delete enums since have special case these two.
[requiredcustom(new int[] {(int)action.update, (int)action.delete)}] public class notes { public string notesid { get; set; } [required] [regularexpression("1|2|3|4")] public int actiontype { get; set; } }
remember, work if set attribute on class, not on notesid property.
validation c#-4.0 model-view-controller asp.net-web-api data-annotations
Comments
Post a Comment