fluentvalidation - AbstractValidator : showing error messages with property values -


i use following pattern when importing "unknown" data.

public class mycustomobject {     public string mycustomdateasstring { get; set; }      public datetime? mycustomdate     {                 {             datetime? returnvalue = null;             datetime parseresult = datetime.minvalue;             bool parseattempt = datetime.tryparse(this.mycustomdateasstring, out parseresult);             if (parseattempt)             {                 returnvalue = parseresult;             }             return returnvalue;         }     }      public string mycustomintasstring { get; set; }       public int? mycustomint     {                 {             int? returnvalue = null;             int parseresult = 0;             bool parseattempt = int.tryparse(this.mycustomintasstring, out parseresult);             if (parseattempt)             {                 returnvalue = parseresult;             }             return returnvalue;         }     }     } 

i have working.

public class mycustomobjectvalidator : abstractvalidator<mycustomobject> {     public mycustomobjectvalidator()     {         rulefor(custobj => custobj.mycustomdateasstring).notempty().withmessage("please specify mycustomdateasstring");         rulefor(custobj => custobj.mycustomintasstring).notempty().withmessage("please specify mycustomintasstring");      } } 

i want add these rules.

        rulefor(custobj => custobj.mycustomdate).notnull().withmessage("mycustomdate must valid non null date.  specified '{0}'"); /* how can put mycustomdateasstring {0} */         rulefor(custobj => custobj.mycustomint).notnull().withmessage("mycustomint must valid non null int.  specified '{0}'"); /* how can put mycustomintasstring {0} */ 

but don't know how mycustomdateasstring , mycustomintasstring show in error messages mycustomdate , mycustomint.

so validators you'd use {propertyvalue} placeholder current property value doesn't in case want value of different property.

however, there's overload of withmessage takes func can use build custom placehold values.

rulefor(x=>x.foo).notnull().withmessage("...blah {0}", x=>x.someotherproperty); 

the final argument params array of func[t,object] can specify many of these like, , they're processed in order.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -