在 ASP.NET MVC4 应用程序中,我们使用 FluentValidation 来验证我们的模型。在某些情况下,我们只想在另一个属性有值时验证一个属性。我们使用 When 关键字来完成此操作。一个简单的验证类如下所示:
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(item => item.FirstName).NotEmpty();
RuleFor(item => item.LastName).NotEmpty().When(item => !string.IsNullOrEmpty(item.FirstName))
}
}
我们希望对此进行客户端验证。我试图创建一个自定义 FluentValidationPropertyValidator。但是我找不到一种方法来获取验证规则的 When 部分。
有人可以指出我正确的方向吗?
最佳答案
FluentValidation 中的一些验证不支持客户端验证:
来自文档( http://fluentvalidation.codeplex.com/wikipage?title=mvc&referringTitle=Documentation ):
Note that FluentValidation will also work with ASP.NET MVC's client-side validation, but not all rules are supported. For example, any rules defined using a condition (with When/Unless), custom validators, or calls to Must will not run on the client side. The following validators are supported on the client:
*NotNull/NotEmpty *Matches (regex) *InclusiveBetween (range) *CreditCard *Email *EqualTo (cross-property equality comparison) *Length
关于asp.net-mvc - 当在客户端时进行流畅的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494440/