如果填充了另一个字段,则 MVC 3 字段为必填字段

2024-01-26

我有一个简单的问题。

例如,我有两个字段映射在模型 ex 上:textbox_1 和 textbox_2。

我想问是否存在一种方法(前强制装饰器),仅当我填充 textbox_1 时才强制执行 textbox_2 。如果我不填写textbox_1,则文本框2是可选的。

有没有一种优雅的方法来做到这一点?


ASP.NET MVC 中没有为此提供开箱即用的解决方案。这是我为解决该问题而创建的属性。该属性有 3 种可用用法:

  • Pass null as targetValue到构造函数:仅当 依赖字段为空。
  • 将任何值传递为tagetValue: 必需的 仅当依赖字段等于传入任何值时。
  • Pass "*" as tagetValue:仅当填充依赖字段时才需要。

在你的情况下,你需要通过"*" as targetValue到构造函数,这意味着依赖属性可以是任何非空值。

Note:它包含服务器端和客户端(+不引人注目)验证。

服务器端属性类:

public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
{
    protected RequiredAttribute _innerAttribute;

    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }

    public bool AllowEmptyStrings
    {
        get
        {
            return _innerAttribute.AllowEmptyStrings;
        }
        set
        {
            _innerAttribute.AllowEmptyStrings = value;
        }
    }

    public RequiredIfAttribute(string dependentProperty, object targetValue)
    {
        _innerAttribute = new RequiredAttribute();
        DependentProperty = dependentProperty;
        TargetValue = targetValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // get a reference to the property this validation depends upon
        var containerType = validationContext.ObjectInstance.GetType();
        var field = containerType.GetProperty(DependentProperty);

        if (field != null)
        {
            // get the value of the dependent property
            var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
            // trim spaces of dependent value
            if (dependentValue != null && dependentValue is string)
            {
                dependentValue = (dependentValue as string).Trim();

                if (!AllowEmptyStrings && (dependentValue as string).Length == 0)
                {
                    dependentValue = null;
                }
            }

            // compare the value against the target value
            if ((dependentValue == null && TargetValue == null) ||
                (dependentValue != null && (TargetValue == "*" || dependentValue.Equals(TargetValue))))
            {
                // match => means we should try validating this field
                if (!_innerAttribute.IsValid(value))
                    // validation failed - return an error
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
            }
        }

        return ValidationResult.Success;
    }

    public virtual IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "requiredif",
        };

        string depProp = BuildDependentPropertyId(metadata, context as ViewContext);

        // find the value on the control we depend on;
        // if it's a bool, format it javascript style 
        // (the default is True or False!)
        string targetValue = (TargetValue ?? "").ToString();
        if (TargetValue is bool)
            targetValue = targetValue.ToLower();

        rule.ValidationParameters.Add("dependentproperty", depProp);
        rule.ValidationParameters.Add("targetvalue", targetValue);

        yield return rule;
    }

    private string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
    {
        // build the ID of the property
        string depProp = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(DependentProperty);
        // unfortunately this will have the name of the current field appended to the beginning,
        // because the TemplateInfo's context has had this fieldname appended to it. Instead, we
        // want to get the context as though it was one level higher (i.e. outside the current property,
        // which is the containing object, and hence the same level as the dependent property.
        var thisField = metadata.PropertyName + "_";
        if (depProp.StartsWith(thisField))
            // strip it off again
            depProp = depProp.Substring(thisField.Length);
        return depProp;
    }
}

客户端(包括不显眼的验证):

$.validator.addMethod('requiredif',
    function (value, element, parameters) {
        var id = '#' + parameters['dependentproperty'];

        // get the target value (as a string, 
        // as that's what actual value will be)
        var targetvalue = parameters['targetvalue'];
        targetvalue = (targetvalue == null ? '' : targetvalue).toString();

        // get the actual value of the target control
        // note - this probably needs to cater for more 
        // control types, e.g. radios
        var control = $(id);
        var controltype = control.attr('type');
        var actualvalue =
            controltype === 'checkbox' ?
            control.attr('checked').toString() :
            control.val();

        // if the condition is true, reuse the existing 
        // required field validator functionality
        if ($.trim(targetvalue) === $.trim(actualvalue) || ($.trim(targetvalue) === '*' && $.trim(actualvalue) !== ''))
            return $.validator.methods.required.call(
              this, value, element, parameters);

        return true;
    });

$.validator.unobtrusive.adapters.add(
    'requiredif',
    ['dependentproperty', 'targetvalue'],
    function (options) {
        options.rules['requiredif'] = {
            dependentproperty: options.params['dependentproperty'],
            targetvalue: options.params['targetvalue']
        };
        options.messages['requiredif'] = options.message;
    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果填充了另一个字段,则 MVC 3 字段为必填字段 的相关文章

随机推荐

  • “constexpr if”与“if”的优化 - 为什么需要“constexpr”?

    C 1z 将引入 constexpr if 根据条件删除一个分支的 if 看起来合理且有用 但是 没有 constexpr 关键字就不可能了吗 我认为在编译期间 编译器应该知道编译期间是否已知条件 如果是的话 即使是最基本的优化级别也应该删
  • 更改 jquery 工具提示箭头的位置

    我正在尝试将箭头的位置更改为文本框附近的左侧 我怎样才能解决这个问题 我已经尝试过这个 工作示例链接 http jsfiddle net b8fcg http jsfiddle net b8fcg HTML
  • Java EE 6 发布日期 [已关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions Java
  • 用于操作 S-Record 和 Intel HEX 16 文件的库

    是否有可用的开源库 用 python 或 java 开发 用于操作 Motorola S Record 文件和 Intel HEX 16 文件 例如从一种格式转换为另一种格式 我正在寻找一个 纯 java或python库 而不仅仅是一组ja
  • RecyclerView 回收时出现问题

    我有一个我使用创建的项目列表RecyclerView 当用户单击其中之一时 我会更改所选项目的背景颜色 问题是 当我滚动浏览我的项目并回收它们时 某些项目会获得所选项目的背景颜色 这是错误的 在这里你可以看到我的Adapter s code
  • 用字典解析字符串的算法

    Given 一本充满单词的字典 in july den dentist best 使用一些 C API 来访问它 boolean findWord string word or string getNextWord void 迭代它 一些没
  • 解码 websocket 框架

    我正在尝试解码 websocket 帧 但在解码扩展有效负载时没有成功 到目前为止我所取得的成就是 char in data char buffer unsigned int i unsigned char mask 4 unsigned
  • 就地对 Perl 数组进行排序

    我有一个对数组的引用 称为 intervals 我想对这个数组中的值进行排序 数组中可能有大量值 所以我不想复制这些值 我目前的做法是这样的 sub by position a gt start lt gt b gt start a gt
  • React Hooks:如何在渲染之前等待数据被获取

    我在 useEffect 挂钩中有 fetch 方法 export const CardDetails gt const card getCardDetails useState const id useParams useEffect g
  • ResponseEntityExceptionHandler 针对 401 异常返回空响应正文

    我正在尝试使用 RestTemplate 实现对身份验证服务器的 Rest 调用 并记录响应 以防服务器返回异常 为了做到这一点 我使用 ResponseEntityExceptionHandler 来处理 HttpClientErrorE
  • R 中的掩码电话号码

    我的原始数据有很多个人信息 所以我在R中屏蔽它们 示例数据和我的原始代码如下 install packages stringr library string x c 010 1234 5678 John 010 8888 8888 Phon
  • Node JS:异步执行命令行并获取输出

    我如何运行命令行并尽快获取输出以将其显示在某处 例如 如果在 Linux 系统上运行 ping 命令 它永远不会停止 现在是否可以在命令仍在处理时获得响应 或者让我们采取apt get install命令 如果我想在安装运行时显示安装进度怎
  • If 语句内部和外部的 Return

    这可能是一个相当容易回答的问题 但它已经困扰我一段时间了 如果 if 语句内有一个 return 语句 在一个方法内 在 Java 语言中 但我在末尾添加另一个作为包罗万象并避免错误 则两个返回值都将在其他 if if 语句为真 一个例子
  • 我如何在Python中只向下舍入数字/浮点数?

    我将生成这个随机数 例如 12 75 或 1 999999999 或 2 65 我希望始终将此数字向下舍入为最接近的整数 因此 2 65 将四舍五入为 2 抱歉 我问了很多遍 但没有找到答案 谢谢 您可以选择我们int math trunc
  • org.bson.BSONObject 中的 java 类型

    我目前正在学习mongodb 的 BSON java 库 http github com mongodb mongo java driver 我正在尝试改变org bson BSONObject到 XML 中 以便将其转换为XSLT样式表
  • 在solr中搜索特殊字符

    我在 solr 中搜索特殊字符时遇到问题 我的文档有一个 标题 字段 有时它可能像 泰坦尼克号 1999 它有字符 当我尝试使用 在 solr 中搜索时 我收到 400 错误 我试图转义这个字符 所以我尝试了 和 之类的东西 经过这些更改
  • C# 中使用掩码进行位操作

    我需要一些有关 C 中位图操作的帮助 我想要一个UInt16 隔离任意数量的位 并使用另一个位设置它们UInt16 value Example 10101010 Original Value 00001100 Mask Isolates b
  • Android将图片旋转90度(相机拍摄)[重复]

    这个问题在这里已经有答案了 我正在通过代码在我的 Samsung Galaxy SII 设备中拍照 保存并在屏幕上显示它后 我看到它旋转了 90 度 我知道这是一些设备问题 并非所有设备上都会发生这种情况 我正在使用给定的相机意图拍照并将其
  • Apollo 客户端什么是主动查询?

    My Setup Typescript 反应应用程序 后端 GraphQL API 阿波罗客户端 使用 Apollo 客户端 Devtools 扩展进行检查 我的问题 什么是主动查询 Apollo 文档谈论了很多主动查询 但我很难得到一个实
  • 如果填充了另一个字段,则 MVC 3 字段为必填字段

    我有一个简单的问题 例如 我有两个字段映射在模型 ex 上 textbox 1 和 textbox 2 我想问是否存在一种方法 前强制装饰器 仅当我填充 textbox 1 时才强制执行 textbox 2 如果我不填写textbox 1