不区分大小写与流畅验证的比较

Case Insensitive Compare with Fluent Validation

我在使用 Fluent Validations 实现不区分大小写的比较时失败了。我只是想比较两个电子邮件字段并忽略大小写。

当前的规则是:

RuleFor(x => x.EmailAddress).NotEmpty().Length(5, 200).EmailAddress();
RuleFor(x => x.ConfirmEmailAddress).NotEmpty().Equal(x => x.EmailAddress).WithMessage("Emails must match");

为了添加不区分大小写的要求,我考虑过使用 equal 调用传递比较器,但这似乎不起作用。

RuleFor(x => x.ConfirmEmailAddress).NotEmpty().Equal(x => x.EmailAddress, StringComparer.CurrentCultureIgnoreCase).WithMessage("Emails must match"); 

理想情况下,如果可能的话,我希望在客户端进行不区分大小写的比较。谁能提供有关如何完成此操作的指导?

我目前使用的NuGet包是:

<package id="FluentValidation" version="5.1.0.0" targetFramework="net45" />
<package id="FluentValidation.MVC4" version="5.1.0.0" targetFramework="net45" />

您可以使用 .Must() 扩展方法,同时接受父对象和 属性 的重载被验证如下:

RuleFor(x => x.ConfirmEmailAddress)
    .NotEmpty()
    .Must((x, confirmEmailAddress) => x.EmailAddress.Equals(confirmEmailAddress, StringComparison.OrdinalIgnoreCase))
    .WithMessage("Emails must match");

这仅在服务器端受支持,但是,根据 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