zoukankan      html  css  js  c++  java
  • MVC 验证规则扩展(当RoleID 属性值为A,B 时,Email 属性必填)

    public class RoleRequiredAttribute : ValidationAttribute, IClientValidatable
        {
            public string  RoleIDS{ get; set; }
    
            public string OtherProperty { get; set; }
    
            public RoleRequiredAttribute(string  roleIDs, string otherProperty)
            {
                RoleIDS = roleIDs;
                OtherProperty = otherProperty;
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                var property = validationContext.ObjectType.GetProperty(OtherProperty);
                if (property == null)
                {
                    return new ValidationResult(string.Format(CultureInfo.CurrentCulture, "{0} 不存在", OtherProperty));
                }
                var otherValue = property.GetValue(validationContext.ObjectInstance, null);
                if (RoleIDS.Replace("",",").Trim(',').Split(',').Contains(otherValue) && value == null)
                {
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
                }
                return null;
            }
             
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                var rule = new ModelClientValidationRule
                {
                    ValidationType = "role",
                    ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
                };
                rule.ValidationParameters["property"] = Newtonsoft.Json.JsonConvert.SerializeObject(new { key = OtherProperty, val = RoleIDS });
                 
                yield return rule;
            }
        }
    (function ($) {    
    $.validator.addMethod("role", function (value, element, param) {
            if (value.replace(/(^s*)|(s*$)/g, "").length != 0) {
                return true;
            }
            else {
                var obj = JSON.parse(param);
                var kv = $("#" + obj["key"]).val();
                if (obj["val"].indexOf(kv) != -1) {
                    return false;
                }
                else {
                    return true;
                }
            }
            return false;
        });
        $.validator.unobtrusive.adapters.addSingleVal("role", "property");
    
    )(jQuery));
    public class  MyModel
    {
    
      [RoleRequired("A,B", "RoleID", ErrorMessage = "必填")]
       public string Email { get; set; }
    
       public string RoleID{ get; set; }
    }
  • 相关阅读:
    爱她就用python给她画个小心心 ♥(ˆ◡ˆԅ)
    用python画小猪佩奇(非原创)
    (解释文)My SQL中主键为0和主键自排约束的关系
    (细节)My SQL中主键为0和主键自排约束的关系
    My SQL常用操作汇总
    博客搬运同步至腾讯云+社区声明
    mysql在ubuntu中的操作笔记(详)
    vim编辑器操作汇总
    linux常用命令汇总
    在python中单线程,多线程,多进程对CPU的利用率实测以及GIL原理分析
  • 原文地址:https://www.cnblogs.com/valeb/p/7989401.html
Copyright © 2011-2022 走看看