FluentValidation 将参数传递给 WithMessage

发布于 2024-12-05 03:06:40 字数 462 浏览 0 评论 0 原文

我在验证器中有以下代码:

RuleFor(mb => mb.Amount).
Must((mb, amount) =>
                {
                   var betLimit = _battlesService.GetBetLimit(mb.BattleId);

                   mb.Amount <= betLimit;
                }).
WithMessage("Bet should be less than {0}", "bet limit value should be placed here");

有没有办法将betLimit 值传递给WithMessage 方法?我看到的唯一解决方案是将betLimit 值设置为ViewModel 的某些属性,然后在WithMessage 重载中使用funcs 访问它。但它很丑。

I have the following code in validator:

RuleFor(mb => mb.Amount).
Must((mb, amount) =>
                {
                   var betLimit = _battlesService.GetBetLimit(mb.BattleId);

                   mb.Amount <= betLimit;
                }).
WithMessage("Bet should be less than {0}", "bet limit value should be placed here");

Is there any way to pass betLimit value to WithMessage method? The only solution I see is to set betLimit value to some property of ViewModel and then access it in WithMessage overload with funcs. But it is ugly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2024-12-12 03:06:40

由于 Amount 不用于获取 betLimit,因此您不能在验证器启动时将下注限制拉入字段中,并在任何您想要的地方使用它吗?类似于:

public ViewModelValidator(IBattlesService battlesService)
{
    var betLimit = battlesService.GetBetLimit();

    RuleFor(mb => mb.Amount).
    Must((mb, amount) =>
                    {
                       mb.Amount <= betLimit;
                    }).
    WithMessage(string.Format("Bet should be less than {0}", "bet limit value should be placed here", betLimit));
    ...
}

更新:

我现在看到您从视图模型中添加了参数。根据 FluentValidation 文档中的第三个示例,看起来您应该能够像这样访问它 此处

    public ViewModelValidator(IBattlesService battlesService)
    {
        RuleFor(mb => mb.Amount).
        Must((mb, amount) =>
                        {
                           mb.Amount <= betLimit;
                        }).
        WithMessage("Bet should be less than {0}", mb => battlesService.GetBetLimit(mb.BattleId));
        ...
    }

Since Amount isn't used to get the betLimit, can't you pull the bet limit into a field when your validator fires up, and use it wherever you want? Something like:

public ViewModelValidator(IBattlesService battlesService)
{
    var betLimit = battlesService.GetBetLimit();

    RuleFor(mb => mb.Amount).
    Must((mb, amount) =>
                    {
                       mb.Amount <= betLimit;
                    }).
    WithMessage(string.Format("Bet should be less than {0}", "bet limit value should be placed here", betLimit));
    ...
}

UPDATE:

I see now that you added the param from the view model. Looks like you should be able to get to it like this, based on the third example in the FluentValidation docs here:

    public ViewModelValidator(IBattlesService battlesService)
    {
        RuleFor(mb => mb.Amount).
        Must((mb, amount) =>
                        {
                           mb.Amount <= betLimit;
                        }).
        WithMessage("Bet should be less than {0}", mb => battlesService.GetBetLimit(mb.BattleId));
        ...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文