ChatGPT解决这个技术问题 Extra ChatGPT

MVC 模型需要 true

有没有办法通过数据注释要求将布尔属性设置为 true?

public class MyAwesomeObj{
    public bool ThisMustBeTrue{get;set;}
}
这到底是什么用例?难道你不能让属性是只读的并一直返回 true 吗?
这几乎可以说......嘿,伙计,你忘了检查我同意......这应该使模型无效。
我认为这是您想要在客户端处理的事情。
@PsychoCoder:应该在双方都处理......不仅仅是客户端。我只是想看看它是否可以通过添加一个简单的数据注释来处理。

a
akousmata

我将为服务器端和客户端创建一个验证器。使用 MVC 和不显眼的表单验证,只需执行以下操作即可实现:

首先,在您的项目中创建一个类来执行服务器端验证,如下所示:

public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable
{
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
        return (bool)value == true;
    }

    public override string FormatErrorMessage(string name)
    {
        return "The " + name + " field must be checked in order to continue.";
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = String.IsNullOrEmpty(ErrorMessage) ? FormatErrorMessage(metadata.DisplayName) : ErrorMessage,
            ValidationType = "enforcetrue"
        };
    }
}

在此之后,在模型中注释适当的属性:

[EnforceTrue(ErrorMessage=@"Error Message")]
public bool ThisMustBeTrue{ get; set; }

最后,通过将以下脚本添加到您的视图来启用客户端验证:

<script type="text/javascript">
    jQuery.validator.addMethod("enforcetrue", function (value, element, param) {
        return element.checked;
    });
    jQuery.validator.unobtrusive.adapters.addBool("enforcetrue");
</script>

注意:我们已经创建了一个方法 GetClientValidationRules,它将我们的注释从我们的模型推送到视图。

如果使用资源文件为国际化提供错误消息,请移除 FormatErrorMessage 调用(或仅调用基础)并调整 GetClientValidationRules 方法,如下所示:

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    string errorMessage = String.Empty;
    if(String.IsNullOrWhiteSpace(ErrorMessage))
    {
        // Check if they supplied an error message resource
        if(ErrorMessageResourceType != null && !String.IsNullOrWhiteSpace(ErrorMessageResourceName))
        {
            var resMan = new ResourceManager(ErrorMessageResourceType.FullName, ErrorMessageResourceType.Assembly);
            errorMessage = resMan.GetString(ErrorMessageResourceName);
        }
    }
    else
    {
        errorMessage = ErrorMessage;
    }

    yield return new ModelClientValidationRule
    {
        ErrorMessage = errorMessage,
        ValidationType = "enforcetrue"
    };
}

谢谢你 - 它工作得很好!删除覆盖 FormatErrorMessage 方法后效果更好 - 这样资源文件中的错误消息的本地化就可以工作。我的用法:[EnforceTrue(ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "TermsAndConditionsRequired")]
我无法让客户端验证正常工作,并且似乎无法说出我做错了什么。我应该把 javacsript 放在哪里?在头部标签中?在控制器旁边?
我同意,这应该是答案
展示自定义验证属性强大功能的绝佳解决方案!虽然我建议将脚本放在全局引用的 js 文件中,而不是视图中,以供重用。此外,最好处理可以添加消息字符串的所有方式:如果未提供,则为默认值,或者消息字符串,或者来自资源文件。
很好的解决方案,感谢发布。对于那些无法让客户端验证工作的人:您必须在加载它将验证的控件之前扩展 jQuery 验证,因此将脚本放在头部,而不是在 window.onload / $(document ).ready() 事件。
O
Owen Johnson

我知道这是一篇较旧的帖子,但想分享一个简单的服务器端方法来做到这一点。您创建一个设置为 true 的公共属性并将您的布尔值与该属性进行比较。如果您的 bool 未选中(默认为 false),则表单将不会验证。

public bool isTrue
{ get { return true; } }

[Required]
[Display(Name = "I agree to the terms and conditions")]
[Compare("isTrue", ErrorMessage = "Please agree to Terms and Conditions")]
public bool AgreeTerms { get; set; }

剃刀代码

@Html.CheckBoxFor(m => Model.AgreeTerms, new { id = "AgreeTerms", @checked = "checked" })
<label asp-for="AgreeTerms" class="control-label"></label>
<a target="_blank" href="/Terms">Read</a>
<br />
@Html.ValidationMessageFor(model => model.AgreeTerms, "", new { @class = "text-danger" })
@Html.HiddenFor(x => Model.isTrue)

+1 为简单起见。仅供参考:我必须公开“isTrue”属性才能使其正常工作。
在 MVC4 中比较不适合我
如果您为“isTrue”属性添加隐藏,您将获得客户端验证
烦人的这个漂亮的解决方案对我不起作用。在 Mvc 5.2.3 上测试。
对我不起作用,因为它最终尝试将 "true""True" 进行比较,其中第一个是复选框值,后者是隐藏字段值(这是布尔 ToString() 结果)
m
moribvndvs

您可以创建自己的验证器:

public class IsTrueAttribute : ValidationAttribute
{
    #region Overrides of ValidationAttribute

    /// <summary>
    /// Determines whether the specified value of the object is valid. 
    /// </summary>
    /// <returns>
    /// true if the specified value is valid; otherwise, false. 
    /// </returns>
    /// <param name="value">The value of the specified validation object on which the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"/> is declared.
    ///                 </param>
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");

        return (bool) value;
    }

    #endregion
}

我会考虑通过客户端实现来增强这一点 - 而不是使用其他答案中提到的远程验证,而是使用此处说明的不显眼:jacopretorius.net/2011/01/client-side-validation-in-mvc-3.html
这对我们来说是一个很好的(并且经过测试的)快速解决方案。我们可以在@dazbradbury 的解决方案(也是一个很好的解决方案)中不进行客户端验证,因为我们只需要在调查的最后一页上的一个单独的复选框上进行验证。
return (bool) value == true; 这是一个多余的比较
K
Kapé

我尝试了几种解决方案,但没有一个完全可以让我同时获得客户端和服务器端验证。所以我在我的 MVC 5 应用程序中做了什么来让它工作:

在您的 ViewModel 中(用于服务器端验证):

public bool IsTrue => true;

[Required]
[Display(Name = "I agree to the terms and conditions")]
[Compare(nameof(IsTrue), ErrorMessage = "Please agree to Terms and Conditions")]
public bool HasAcceptedTermsAndConditions { get; set; }

在您的 Razor 页面中(用于客户端验证):

<div class="form-group">
   @Html.CheckBoxFor(m => m.HasAcceptedTermsAndConditions)
   @Html.LabelFor(m => m.HasAcceptedTermsAndConditions)
   @Html.ValidationMessageFor(m => m.HasAcceptedTermsAndConditions)

   @Html.Hidden(nameof(Model.IsTrue), "true")
</div>

迷人的解决方案!
注意隐藏字段的值(“true”)!
R
Ruskin

我只想将人们引向以下小提琴:https://dotnetfiddle.net/JbPh0X

用户将 [Range(typeof(bool), "true", "true", ErrorMessage = "You gotta tick the box!")] 添加到他们的布尔属性中,这会导致服务器端验证工作。

为了让客户端验证也能正常工作,他们添加了以下脚本:

// extend jquery range validator to work for required checkboxes
var defaultRangeValidator = $.validator.methods.range;
$.validator.methods.range = function(value, element, param) {
    if(element.type === 'checkbox') {
        // if it's a checkbox return true if it is checked
        return element.checked;
    } else {
        // otherwise run the default validation function
        return defaultRangeValidator.call(this, value, element, param);
    }
}

t
ta.speot.is

只需检查它的字符串表示是否等于 True

[RegularExpression("True")]
public bool TermsAndConditions { get; set; }

@JeradRose 在服务器上验证得很好。您指的是客户端验证吗?
确认,这适用于服务器端,但不适用于客户端
我认为服务器端验证可能存在类型不匹配异常,试图将布尔值与字符串进行比较。
RegularExpressionAttribute 在内部使用 Convert.ToString 来获取属性值的字符串表示形式(作为 object 传递给它)。
我认为这个答案比我的@fields-cage +1 简单
M
Matthew Manela

您可以创建自己的属性或使用 CustomValidationAttribute

这就是您将如何使用 CustomValidationAttribute:

[CustomValidation(typeof(BoolValidation), "ValidateBool")]

其中 BoolValidation 定义为:

public class BoolValidation
{
  public static ValidationResult ValidateBool(bool boolToBeTrue)
  {
    if (boolToBeTrue)
    {
      return ValidationResult.Success;
    }
    else
    {
      return new ValidationResult(
          "Bool must be true.");
    }
  }

G
George Stocker

[Required] 属性代表要求 any 值 - 它可以是 true 或 false。您必须为此使用另一个验证。


M
Matt

对于 ASP.NET Core MVC,这里是客户端和服务器验证,基于 dazbradbury 的解决方案

public class EnforceTrueAttribute : ValidationAttribute, IClientModelValidator
{
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
        return (bool)value;
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        MergeAttribute(context.Attributes, "data-val", "true");
        var errorMessage = ErrorMessage ?? 
            $"The value for field {context.ModelMetadata.GetDisplayName()} must be true.";
        MergeAttribute(context.Attributes, "data-val-enforcetrue", errorMessage);
    }

    private void MergeAttribute(IDictionary<string, string> attributes,
        string key,
        string value)
    {
        if (attributes.ContainsKey(key))
        {
            return;
        }
        attributes.Add(key, value);
    }
}

然后在客户端:

$.validator.addMethod("enforcetrue", function (value, element, param) {
    return element.checked;
});

$.validator.unobtrusive.adapters.addBool("enforcetrue");

那么用法是:

[EnforceTrue(ErrorMessage = "Please tick the checkbox")]
public bool IsAccepted { get; set; }

使用此解决方案时,请考虑 this。将 javascript 代码放在任何“jquery $document.ready()/$(function() { });”之外。
另一个提示,不要将 required 属性放在 HTML 输入中,例如:<input asp-for="..." class="..." id="..." type="checkbox" required/>
d
david thompson

跟进 ta.speot.is 的帖子和 Jerad Rose 的评论:

给定的帖子将无法通过不显眼的验证在客户端工作。这应该适用于两个阵营(客户端和服务器):

[RegularExpression("(True|true)")]
public bool TermsAndConditions { get; set; }

不知道这是否是较新版本的问题,但它不适用于 jquery.validate 1.19.2 和 jquery.validate.unobtrusive 3.2.11。问题似乎是 regex 方法 unobtrusive 在验证正则表达式之前首先检查复选框是否是可选的,这是有道理的,除了 jquery.validate 似乎认为任何未选中的复选框都是可选的。 tl; dr 它只在选中的复选框上运行正则表达式。我们可以为 regex validator 方法添加一个填充程序,或者只创建一个自定义验证器。
A
Aung San Myint

.NET Core MVC - 带有数据注释的必需复选框

public class MyModel
{
    [Display(Name = "Confirmation")]
    [Range(typeof(bool), "true", "true", ErrorMessage = "Please check the Confirmation checkbox.")]
    public bool IsConfirmed { get; set; }   
}

<div class="custom-control custom-checkbox col-10">
    <input type="checkbox" asp-for="IsConfirmed" class="custom-control-input" />
    <label class="custom-control-label" for="IsConfirmed">
        "By clicking 'submit', I confirm."
    </label>
    <span asp-validation-for="IsConfirmed" class="text-danger"></span>
</div>

<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        // extend range validator method to treat checkboxes differently
        var defaultRangeValidator = $.validator.methods.range;
        $.validator.methods.range = function (value, element, param) {
            if (element.type === 'checkbox') {
                // if it's a checkbox return true if it is checked
                return element.checked;
            } else {
                // otherwise run the default validation function
                return defaultRangeValidator.call(this, value, element, param);
            }
        }
    });
</script>

C
Chase Florell

我不知道通过 DataAnnotations 的方法,但这很容易在您的控制器中完成。

public ActionResult Add(Domain.Something model)
{

    if (!model.MyCheckBox)
        ModelState.AddModelError("MyCheckBox", "You forgot to click accept");

    if (ModelState.IsValid) {
        //'# do your stuff
    }

}

唯一的其他选择是为服务器端构建一个自定义验证器,为客户端构建一个 remote validator(远程验证仅在 MVC3+ 中可用)


Kinda 已经是新的如何检查布尔标志了......想知道是否有它的数据注释。
C
Community

你有appropriate items set up in the web.config吗?

这可能会导致验证不起作用。

您还可以尝试创建自定义验证属性(因为 [Required] 只关心它是否存在,而您关心值):

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class RequiredTrueAttribute : ValidationAttribute
{
    // Internal field to hold the mask value.
    readonly bool accepted;

    public bool Accepted
    {
        get { return accepted; }
    }

    public RequiredTrueAttribute(bool accepted)
    {
        this.accepted = accepted;
    }

    public override bool IsValid(object value)
    {
        bool isAccepted = (bool)value;
        return (isAccepted == true);
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentCulture,
          ErrorMessageString, name, this.Accepted);
    }
}

然后,用法:

[RequiredTrue(ErrorMessage="{0} requires acceptance to continue.")]
public bool Agreement {get; set;}

here


t
toddmo

这对我有用。其他什么都没做。 MV 5:

模型

public string True
{
  get
  {
    return "true";
  }
}

[Required]
[Compare("True", ErrorMessage = "Please agree to the Acknowlegement")]
public bool Acknowlegement { get; set; }

看法

  @Html.HiddenFor(m => m.True)
  @Html.EditorFor(model => model.Acknowlegement, new { htmlAttributes = Model.Attributes })
  @Html.ValidationMessageFor(model => model.Acknowlegement, "", new { @class = "text-danger" })

https://i.stack.imgur.com/ananr.png

https://i.stack.imgur.com/JJExG.png


D
Dronz

我尝试使用 fields.cage 的答案,但它对我来说不太管用,但更简单的方法可以做到,我不确定为什么(可能是不同的 Razor 版本?),但我所要做的就是:

[Required]
[Range(typeof(bool), "true", "true", ErrorMessage = "Agreement required.")]
[Display(Name = "By clicking here, I agree that my firstborn child will etc etc...")]
public bool Agreement1Checked { get; set; }

在 .cshtml 文件中:

@Html.CheckBoxFor(m => m.Agreement1Checked)
@Html.LabelFor(m => m.Agreement1Checked)
@Html.ValidationMessageFor(m => m.Agreement1Checked)

这对我来说在客户端不起作用。由于某种原因,传递给 jquery.validate 规则方法的参数是 [NaN, NaN],它应该是 [true, true]
@xr280xr 即使用户选中了复选框?
s
samack

我认为处理此问题的最佳方法是检查您的控制器,如果该框为真,否则只需向您的模型添加一个错误并让它重新显示您的视图。

如前所述,所有 [Required] 所做的都是确保有一个值,在您的情况下,如果未选中,您仍然会得到错误。


D
DavidWainwright

查看万无一失的验证 here。您可以通过 Nuget 下载/安装它。

对于这类事情来说,这是一个很棒的小图书馆。


嗯……虽然默认验证属性工作得很好。
P
Pang
/// <summary> 
///  Summary : -CheckBox for or input type check required validation is not working the root cause and solution as follows
///
///  Problem :
///  The key to this problem lies in interpretation of jQuery validation 'required' rule. I digged a little and find a specific code inside a jquery.validate.unobtrusive.js file:
///  adapters.add("required", function (options) {
///  if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
///    setValidationValues(options, "required", true);
///    }
///   });
///   
///  Fix: (Jquery script fix at page level added in to check box required area)
///  jQuery.validator.unobtrusive.adapters.add("brequired", function (options) {
///   if (options.element.tagName.toUpperCase() == "INPUT" && options.element.type.toUpperCase() == "CHECKBOX") {
///              options.rules["required"] = true;
///   if (options.message) {
///                   options.messages["required"] = options.message;
///                       }
///  Fix : (C# Code for MVC validation)
///  You can see it inherits from common RequiredAttribute. Moreover it implements IClientValidateable. This is to make assure that rule will be propagated to client side (jQuery validation) as well.
///  
///  Annotation example :
///   [BooleanRequired]
///   public bool iAgree { get; set' }
/// </summary>


public class BooleanRequired : RequiredAttribute, IClientValidatable
{

    public BooleanRequired()
    {
    }

    public override bool IsValid(object value)
    {
        return value != null && (bool)value == true;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        return new ModelClientValidationRule[] { new ModelClientValidationRule() { ValidationType = "brequired", ErrorMessage = this.ErrorMessage } };
    }
}

虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。
它有效检查此链接,说明验证失败的原因-itmeze.com/2010/12/06/…
今天它起作用了。你能确定它会在 5 年、10 年后继续工作吗?这些问答数据库也是为未来的用户创建的