RuleFor(x => x.Surname).NotNull().DependentRules(() =>{ RuleFor(x=>x.Forename).NotNull(); }); 现在只有当Surname 的验证通过了才会执行Forname的验证。 通常情况下这个验证器不推荐使用,可以用When来替代它。 回调 验证失败后你可以用OnFailure或者OnAnyFailure来进行回调。 RuleFor(x => x.Surname...
RuleFor(x => x.MaxNumberTeamMembers) .NotEmpty() .WithMessage("Max. number of team members is required") .Must(x => x.Value > 0) .When(x => x.MaxNumberTeamMembers != null) .WithMessage("Max. number of team members must be greater than 0");更新: 以下...
RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount); RuleFor(customer => customer.Address).Length(20, 250); RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode"); } private bool BeAValidPostcode(strin...
RuleFor(customer =>customer.Forename).NotEmpty().WithMessage("Please specify a first name");RuleFor(customer => customer.Company).NotNull();RuleFor(customer =>customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);RuleFor(customer =>customer.Address).Length(20, 250...
我在使用.When()子句时遇到了问题,因为我无法让它工作。但是,我可以让.NotEmpty()和.Length()这样的子句正常工作。public ViewModelEmployerValidator() RuleFor(x => x.NewLineManagerEmail).NotEmpty().WhenHtml.ValidationMes 浏览1提问于2016-10-17得票数 3 回答已采纳...
RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name"); RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount); RuleFor(customer => customer.Address).Length(20, 250); ...
.When(x => x.Name != null) .InvalidWhen(x => !string.IsNullOrEmpty(x.Name)) .WithMessage("ID must be greater than 0 when Name is not null or empty."); RuleFor(x => x.Name) .NotNull() .WithMessage("Name is required."); ...
.NotNull() .When(p => (DateTime.Now > p.Birthday.AddYears(1))) .WithMessage(p => $"出生日期为{p.Birthday},现在时间为{DateTime.Now},大于一岁,CardID值必填!"); RuleFor(p => p.Tel).NotNull().Matches(@"^(\d{3,4}-)?\d{6,8}$|^[1]+[3,4,5,8]+\d{9}$"); ...
这是一个数组索引从0开始的混乱情况。你设定的条件是
Note that not all rules defined in FluentValidation will work with ASP.NET's client-side validation. For example, any rules defined using a condition (with When/Unless), custom validators, or calls toMustwill not run on the client side. Nor will any rules in aRuleSet(although this can be...