Forums - C# Corner

Forum guidelines
darma teja

darma teja

  • 1.1k
  • 429
  • 180.3k

create custom validator-fluentvalidation

Oct 6 2017 9:39 AM
I would like to create fluentValidation cutomer validator, So that I can use this custom validator in coming projects.
 
My class is and xxxxxxxxx-want to get custom method after implementation of code (I tried but not giving +ve output-see my following code)
  1. namespace  ratenamespace  
  2. {  
  3. public class ratecmd  
  4. {  
  5. [NoODataProcedureParameter]  
  6. public int? ProductId { getset; }  
  7.   
  8. [NoODataProcedureParameter]  
  9. public int? PlaceId { getset; }  
  10.   
  11. [NoODataProcedureParameter]  
  12. public int? Rate { getset; }  
  13. }  
  14.   
  15. public class ratecmdValidator : AbstractValidator  
  16.     {  
  17.         public ratecmd()  
  18.         {  
  19.             RuleFor(x => x.Rate).xxxxxxxxx;  
  20. }}} 
I have created seperate class for custom validator like this:
  1. public class myValidator : PropertyValidator  
  2.     {  
  3.         public  myValidator()  
  4.             : base("{PropertyName} must be greater than zero and not null.") { }  
  5.   
  6.         protected override bool IsValid(PropertyValidatorContext context)  
  7.         {  
  8.             int? rate = context.PropertyValue as int?;  
  9.             if (rate != null && rate <= 0)  
  10.             {  
  11.                 return false;  
  12.             }  
  13.             return true;  
  14.         }  
  15.     }

Answers (1)