FluentValidation Call RuleSet and Common Rules

前端 未结 5 804
无人共我
无人共我 2021-02-08 04:24

I have the following class

public class ValidProjectHeader : AbstractValidator
    {
        public ValidProjectHeader()
        {         


        
5条回答
  •  遇见更好的自我
    2021-02-08 04:30

    I have found one way to do it by adding a second validator.Validate to the CallValidation(string ruleSet) method it is as follows

    public virtual bool CallValidation(string ruleSet)
            {
                Errors = new List();
                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;
            }
    

提交回复
热议问题