使用 xVal 验证必须为 true 的 bool

发布于 2024-08-18 19:57:15 字数 293 浏览 5 评论 0原文

我有一个业务要求,要求在允许提交表单之前强制将 HTML 表单上的复选框标记为 true。

如果尚未使用适当的消息选中此框,但希望同时从表单数据的 xVal 验证中返回所有信息,我可以将用户返回到表单。

我在其他地方找不到任何信息,因此是否可以使用 xVal 来验证 bool 为 true(或 false),类似于使用 [Range(min, max)] DataAnnotation 或者我必须手动 .AddModelError(..) 包含此信息以将错误添加到 ViewModel?

I have a business requirement to enforce a check box on a HTML form to be marked as true before allowing submission of the form.

I can return the user to the form if this box has not been checked with an appropriate message, but want to return all information from an xVal validation of the form data at the same time.

I can't find any information elsewhere, so is it possible to use xVal to validate a bool to true (or false), similar to using the [Range(min, max)] DataAnnotation or must I manually .AddModelError(..) containing this information to add the error to the ViewModel?

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

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

发布评论

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

评论(2

深者入戏 2024-08-25 19:57:15

您是否尝试过创建自己的 ValidationAttribute?我为这种情况创建了一个 TrueTypeAttribute。

using System;
using System.ComponentModel.DataAnnotations;

namespace KahunaCentralMVC.Data.ModelValidation.CustomValidationAttributes
{
    public class TrueTypeAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null) return false;
            bool newVal;
            try
            {
                newVal = Convert.ToBoolean(value);
                if (newVal)
                    return true;
                else
                    return false;
            }
            catch (InvalidCastException)
            {
                return false;
            }
        }
    }
}

[MetadataType(typeof(FooMetadata))]
public partial class Foo
{
    public class FooMetadata
    {
        [Required(ErrorMessage = " [Required] ")]
        [TrueTypeAttribute(ErrorMessage = " [Required] ")]
        public bool TruVal { get; set; }
    }
}

Have you tried creating your own ValidationAttribute? I created a TrueTypeAttribute for this sort of situation.

using System;
using System.ComponentModel.DataAnnotations;

namespace KahunaCentralMVC.Data.ModelValidation.CustomValidationAttributes
{
    public class TrueTypeAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null) return false;
            bool newVal;
            try
            {
                newVal = Convert.ToBoolean(value);
                if (newVal)
                    return true;
                else
                    return false;
            }
            catch (InvalidCastException)
            {
                return false;
            }
        }
    }
}

[MetadataType(typeof(FooMetadata))]
public partial class Foo
{
    public class FooMetadata
    {
        [Required(ErrorMessage = " [Required] ")]
        [TrueTypeAttribute(ErrorMessage = " [Required] ")]
        public bool TruVal { get; set; }
    }
}
最近可好 2024-08-25 19:57:15

xVal 处理复选框上的必填字段数据注释,因为必须选中。我最近必须解决这种情况,因为我试图表示一个不可为空的布尔值,其中复选框可以为真或为假(只是不为空)。但就你而言,这很完美。但是,它提供了必填字段验证消息,您可能正在寻找“必须接受这些条款”类型的消息。

使用 xval 远程规则验证并从 ajax 资源进行验证可能是最简单的。

xVal treats a required field dataannotation on a checkbox, as must be checked. I had to work around this situation recently as I was trying to represent a non-nullable boolean where the checkbox could be true or false (just not null). But in your case this works out perfectly. However, it gives a required field validation message where you are perhaps looking for a "must accept these terms" type message.

It might be easiest to use the xval remote rule validation, and validate from an ajax resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文