ho la seguente classechiamata set di regole e norme comuni 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());
});
}
e quello che sto cercando di fare è chiamare la convalida con il rulset ma voglio anche tornare alle regole "comuni" quando chiamo il validazione con RuleSet.
del codice che ho per chiamare la validazione è il seguente
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>();
}
}
posso chiamare il metodo CallValidation
con il membro ruleSet
ma non chiama anche le regole "comuni".
So che posso creare un set di regole "comune" per l'esecuzione di queste regole, ma in tal caso dovrei chiamare la convalida con il set di regole comune sempre.
C'è un modo per chiamare il set di regole e anche chiamare le regole comuni.
Questa opzione non sembra esistere nel codice sorgente git almeno dal 2009, che è quello che mi ha portato qui poiché la documentazione non è corretta. – john
Ottimo consiglio! In FluentValidation 6.2.1 il caso argomento RuleSet è diverso: "ruleSet:" invece di "Ruleset:". Esempio: validator.Validate (obj, ruleSet: "default, Insert"); –