2012-12-14 19 views
6

Tôi có lớp sauGọi ruleset và Common Rules FluentValidation

public class ValidProjectHeader : AbstractValidator<Projects.ProjectHeader> 
    { 
     public ValidProjectHeader() 
     { 

      RuleFor(x => x.LobId).Must(ValidateLOBIDExists); 
      RuleFor(x => x.CreatedByUserId).NotEmpty(); 
      RuleFor(x => x.ProjectManagerId).NotEmpty(); 
      RuleFor(x => x.ProjectName).NotEmpty(); 
      RuleFor(x => x.SalesRepId).NotEmpty(); 
      RuleFor(x => x.DeliveryDate).NotEmpty(); 
      RuleFor(x => x.ProjectStatusId).NotEmpty(); 
      RuleFor(x => x.DeptartmentId).NotEmpty(); 
      RuleFor(x => x.CustomerId).NotEmpty(); 

      RuleSet("Insert",() => 
      { 
       RuleFor(x => x.ProjectLines).Must(ValidateProjectLines).SetCollectionValidator(new ValidProjectLine()); 
      }); 
      RuleSet("Update",() => 
      { 
       RuleFor(x => x.ProjectLines).SetCollectionValidator(new ValidProjectLine()); 
      }); 


     } 

và những gì tôi đang cố gắng làm là gọi xác nhận với rulset nhưng tôi cũng muốn quay trở lại các quy tắc "chung" khi tôi gọi xác nhận hợp lệ với RuleSet.

Bộ luật tôi đã cho gọi xác nhận như sau

public abstract class BaseValidator 
    { 
     private List<ValidationFailure> _errors; 
     public bool IsValid { get; protected set; } 
     public List<ValidationFailure> Errors 
     { 
      get { return _errors; } 
      protected set { _errors = value; } 
     } 
     public virtual bool CallValidation() 
     { 
      Errors = new List<ValidationFailure>(); 
      ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute; 
      IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator; 
      FluentValidation.Results.ValidationResult result = validator.Validate(this); 
      IsValid = result.IsValid; 
      Errors = result.Errors.ToList(); 
      return result.IsValid; 
     } 

     public virtual bool CallValidation(string ruleSet) 
     { 
      Errors = new List<ValidationFailure>(); 
      ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute; 
      IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator; 
      FluentValidation.Results.ValidationResult result = validator.Validate(new FluentValidation.ValidationContext(this, new PropertyChain(), new RulesetValidatorSelector(ruleSet))); 
      IsValid = result.IsValid; 
      Errors = result.Errors.ToList(); 
      return result.IsValid; 
     } 

     public BaseValidator() 
     { 
      Errors = new List<ValidationFailure>(); 
     } 
    } 

tôi có thể gọi phương pháp CallValidation với thành viên ruleSet nhưng nó không được gọi các quy tắc "chung" cũng có.

Tôi biết tôi có thể tạo một "Quy tắc" phổ biến để chạy các quy tắc này nhưng trong trường hợp đó tôi sẽ phải gọi xác nhận bằng Common RuleSet luôn.

Có cách nào tôi có thể gọi cho RuleSet và cũng gọi các quy tắc chung.

Trả lời

3

Tôi đã tìm thấy một cách để làm điều đó bằng cách thêm một giây validator.Validate đến CallValidation(string ruleSet) phương pháp đó là như sau

public virtual bool CallValidation(string ruleSet) 
     { 
      Errors = new List<ValidationFailure>(); 
      ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute; 
      IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator; 
      FluentValidation.Results.ValidationResult result = validator.Validate(new FluentValidation.ValidationContext(this, new PropertyChain(), new RulesetValidatorSelector(ruleSet))); 
      FluentValidation.Results.ValidationResult resultCommon = validator.Validate(this); 
      IsValid = (result.IsValid && resultCommon.IsValid); 
      Errors = result.Errors.Union(resultCommon.Errors).ToList(); 
      return IsValid; 
     } 
7

Thay vào đó bạn có thể làm điều này:

FluentValidation.Results.ValidationResult resultCommon = validator.Validate(parameter, Ruleset : "default, Insert"); 
+0

Tùy chọn này dường như không tồn tại trong mã nguồn git kể từ ít nhất năm 2009, đó là điều khiến tôi ở đây vì tài liệu không chính xác. – john

+0

Lời khuyên tuyệt vời! Trong FluentValidation 6.2.1, trường hợp đối số RuleSet là khác nhau: "ruleSet:" thay vì "Ruleset:". Ví dụ: validator.Validate (obj, ruleSet: "default, Insert"); –

6

Trong lớp Validator của bạn tạo một phương thức, bao gồm tất cả các quy tắc "phổ biến" cần được áp dụng mọi lúc. Bây giờ bạn có thể gọi phương pháp này

  • từ bạn ruleset "tạo ra"
  • từ bên ngoài ruleset

Ví dụ

public class MyEntityValidator : AbstractValidator<MyEntity> 
{ 
    public MyEntityValidator() 
    { 
     RuleSet("Create",() => 
      { 
       RuleFor(x => x.Email).EmailAddress(); 
       ExecuteCommonRules(); 
      }); 

     ExecuteCommonRules(); 
    } 

    /// <summary> 
    /// Rules that should be applied at all times 
    /// </summary> 
    private void ExecuteCommonRules() 
    { 
     RuleFor(x => x.Name).NotEmpty(); 
     RuleFor(x => x.City).NotEmpty(); 
    } 
} 

Bạn xác định ruleset cho một hành động trong điều khiển của bạn

[HttpPost] 
public ActionResult Create([CustomizeValidator(RuleSet = "Create")] MyEntity model) 

Điều này sẽ đảm bảo rằng yêu cầu hành động Tạo sẽ được xác nhận với Quy tắc Tạo Quy tắc. Tất cả các hành động khác sẽ sử dụng cuộc gọi đến ExecuteCommonRules trong bộ điều khiển.

Các vấn đề liên quan