c# - Custom Validation Attribute reading from a form field -


i trying create own validation attribute isunique checks existing values given property. understand isvalid() must overridden custom validation attribute can work. far have seen examples validate attributes take string parameters compared hard coded values inside isvalid() method.

i need isvalid() method access property , value further compare values in database.

this have done far:

public class isunique : validationattribute {     private string codeno { get; set; }             : base("{0} in use")      public isunique (string codeno)     {         this.codeno = codeno;     }      public override validationresult isvalid(object value,                                              validationcontext vcontext)     {         if (value != null)         {             mydbcontext db = new mydbcontext();             student studentscodeno =                      db.students.firstordefault(r => r.codeno== (string)value);             if (studentscodeno != null)             {                  string errormessage =                         formaterrormessage(vcontext.displayname);                 return new validationresult(errormessage);             }         }         return validationresult.success;     } } 

as said, problem version takes parameter. codeno read user form field, , such value compared against in database. don't know how read values form fields.

here code

public class isunique : validationattribute{   public override validationresult isvalid(object value,                                          validationcontext vcontext) {      propertyinfo property = validationcontext.objecttype.getproperty("codeno");     if (property == null)          return new validationresult(string.format("property '{0}' undefined","codeno"));       var fieldvalue = property.getvalue(validationcontext.objectinstance, null);      string codeno= (fieldvalue == null ? "" : fieldvalue.tostring());     if (!string.isnullorempty(codeno))     {         mydbcontext db = new mydbcontext();         student studentscodeno =                  db.students.firstordefault(r => r.codeno== codeno);         if (studentscodeno != null)         {              string errormessage =                     formaterrormessage(vcontext.displayname);             return new validationresult(errormessage);         }     }     return validationresult.success;    }} 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -