【问题标题】:Fluent validation NotEqual is not trigger流利验证 NotEqual 未触发
【发布时间】:2020-05-25 10:55:43
【问题描述】:

我希望通过 Fluent Validation 验证二级密码不能与第一个密码相同

RuleFor(x => x.FirstPassword).NotEqual(x => x.SecondPassword).WithMessage("Second password are not allow to same as first password.");

FirstPassword 输入 => 123456

第二个密码输入 => 123456

都输入了相同的密码,但验证没有触发

更新

使用Must尝试另一种方式,但仍然无法触发

RuleFor(x => x.FirstPassword).Must((x, y) => y != x.SecondPassword).WithMessage("Second password are not allow to same as first password.");

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-core fluentvalidation


【解决方案1】:

您可以尝试如下:

RuleFor(u => u)
.Must(u => u.FirstPassword != u.SecondPassword)
.WithMessage("Second password are not allow to same as first password.");

测试结果:

【讨论】:

  • @FeelRightz 对不起,我误会了。如果您希望辅助密码不能与第一个密码相同,只需将 == 更改为 != 。
【解决方案2】:
RuleFor(x => x.FirstPassword) .NotEmpty().WithMessage(localizer["{PropertyName} must not be empty."]) .MinimumLength(8);

RuleFor(x => x.SecondPassword) .NotEmpty().WithMessage(localizer["{PropertyName} must not be empty."]) .NotEqual(x => x.Password).WithMessage(localizer["{PropertyName} do not match."]);

【讨论】:

  • 你也可以这样考虑
猜你喜欢
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多