FluentValidation 可以做复选框吗?

发布于 2024-12-15 08:42:10 字数 1129 浏览 0 评论 0原文

我正在使用 FluentValidation 来验证 MVC 表单上的输入。

我有一个复选框。

无论我将验证规则设置为什么,它都不会验证该复选框。

我知道验证正在工作,因为我有一个下拉列表可以在同一页面上正常验证。

查看

<%: Html.CheckBoxFor(m => m.fullname_required) %>

模型

[Validator(typeof(CreateFormModelValidator))]
public class CreateFormModel
{
    public int? group_id { get; set; }

    public IEnumerable<SelectListItem> Groups { get; set; }

    [DisplayName("Fullname is required")]
    public bool fullname_required { get; set; }
}
public class CreateFormModelValidator : AbstractValidator<CreateFormModel>
{
    public CreateFormModelValidator()
    {
        RuleFor(x => x.group_id).NotEmpty().NotNull().WithMessage("Please select a group!");
        RuleFor(x => x.fullname_required).NotNull();        
    }
}

我已经尝试过,

RuleFor(x => x.fullname_required).NotEmpty();
RuleFor(x => x.fullname_required).NotNull();
RuleFor(x => x.fullname_required).NotEqual(false); 

但都不起作用。我正在疯狂地尝试让这项工作成功。这是一个该死的复选框

PS:我发现有讨论使用 jQuery 的线程,但这是使用服务器端验证,而不是客户端。

I'm using FluentValidation to validate input on an MVC form.

I have a checkbox.

No matter what I set the validation rule to, it does not validate the checkbox.

I know validation is working, because I have a dropdownlist that's validating fine on the same page.

View

<%: Html.CheckBoxFor(m => m.fullname_required) %>

Model

[Validator(typeof(CreateFormModelValidator))]
public class CreateFormModel
{
    public int? group_id { get; set; }

    public IEnumerable<SelectListItem> Groups { get; set; }

    [DisplayName("Fullname is required")]
    public bool fullname_required { get; set; }
}
public class CreateFormModelValidator : AbstractValidator<CreateFormModel>
{
    public CreateFormModelValidator()
    {
        RuleFor(x => x.group_id).NotEmpty().NotNull().WithMessage("Please select a group!");
        RuleFor(x => x.fullname_required).NotNull();        
    }
}

I've tried

RuleFor(x => x.fullname_required).NotEmpty();
RuleFor(x => x.fullname_required).NotNull();
RuleFor(x => x.fullname_required).NotEqual(false); 

None of which work. I'm going bananas trying to make this work. It's an F***ing checkbox

PS: I've found threads talking about using jQuery instead, but this is using server side validation, not client side.

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

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

发布评论

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

评论(3

捂风挽笑 2024-12-22 08:42:10

我从未将 ValidationMessageFor 属性放置在复选框的页面上。验证正常,但我看不到。

更新:
最终目标是拥有一个复选框,仅当选中第二个复选框时才验证该复选框。即

RuleFor(x => x.fullname_show).NotEmpty().When(x => x.fullname_required == true);

,但是,在上面的facepalm之后这仍然不起作用,但是感谢lomaxx,我能够通过将fullname_required转换为bool来让它工作?

RuleFor(x => x.fullname_show).NotEmpty().When(x => ((bool?)x.fullname_required) != false);

如果有人尝试在 MVC 中使用 FluentValidation 有条件地验证复选框,那么您就是这样做的!

I never placed the ValidationMessageFor attribute on the page for the checkbox. It was validating fine, but I couldn't see it.

UPDATE:
The eventual goal was to have one checkbox which is only validated if a second checkbox is checked. Ie

RuleFor(x => x.fullname_show).NotEmpty().When(x => x.fullname_required == true);

However, this still wouldn't work after the facepalm above, but thanks to lomaxx I was able to get it working by converting fullname_required to bool?

RuleFor(x => x.fullname_show).NotEmpty().When(x => ((bool?)x.fullname_required) != false);

If anyone is trying to conditionally validate a checkbox using FluentValidation in MVC, THAT'S how you do it!

七分※倦醒 2024-12-22 08:42:10

我有一个理论,我可能是错的,但 bool 是一种值类型,而不是引用类型,因此根据定义它总是有一个值(即 true 或 false)。

因此,当您提交表单时,它会看到“未选中”的值为 false,并假定这是一个有效值。

您可能需要做的是将代码中的 bool 更改为可为空,以便默认值为“null”而不是 false。

I have a theory, and I could be wrong, but a bool is a value type, not a reference type, so by definition it always has a value (i.e. true or false).

So when you're submitting the form, it sees it has a value of false for "unchecked" and assumes that's a valid value.

What you might need to do is change the bool to be nullable in your code so that the default value is "null" rather than false.

鱼忆七猫命九 2024-12-22 08:42:10

要使用 Fluent 验证来验证复选框,您可以在验证时检查字段的值。我已经测试过它工作正常,没有任何缺陷。

 RuleFor(x => x.fullname_required).Must(x => x.Equals(true)).WithMessage("Full Name is Required");

For validating your check box using Fluent validation, you can use check the value of your field while validating. I have tested it and it is working fine without any flaw.

 RuleFor(x => x.fullname_required).Must(x => x.Equals(true)).WithMessage("Full Name is Required");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文