【问题标题】:Custom validation attribute doesn't work with custom tag helper, only static html自定义验证属性不适用于自定义标签助手,仅适用于静态 html
【发布时间】:2020-09-24 16:49:06
【问题描述】:

我一直在尝试实现这一点,但没有运气。我 为我的项目中的文本框构建了一个自定义标签助手并正在使用它 像这样:

<text-box asp-for="@Model.PhoneNumber"></text-box>

问题是我还创建了一个自定义验证属性:

public class InputFormatAttribute : ValidationAttribute, IClientModelValidator
{
    public EInputFormat InputFormat { get; }

    public InputFormatAttribute(EInputFormat format) : base()
    {
        InputFormat = format;
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        string inputMask = "";
        string secondAttribute = "";
        switch (InputFormat)
        {
            case EInputFormat.BranchNumber:
                inputMask = @"'alias': 'GCIS-branch-number'";
                break;
            case EInputFormat.BusinessNumber:
                inputMask = @"'alias': 'GCIS-business-number'";
                break;
            case EInputFormat.DirectDepositAccountNumber:
                inputMask = "'alias': 'GCIS-direct-deposit-account-number'";
                break;
            case EInputFormat.Email:
                secondAttribute = "type = email";
                break;
            case EInputFormat.FullBusinessNumber:
                inputMask = "'alias': 'GCIS-full-business-number'";
                break;
            case EInputFormat.Integer:
                inputMask = "'alias': 'GCIS-integer'";
                break;
            case EInputFormat.InternationalPhoneNumber:
                inputMask = "'alias': 'GCIS-integer'";
                secondAttribute = "type=tel";
                break;
            case EInputFormat.Percentage:
                inputMask = "'alias': 'GCIS-percentage'";
                break;
            case EInputFormat.PhoneNumber:
                inputMask = "'alias': 'GCIS-phone-number'";
                secondAttribute = "data-rule-phoneUS = true";
                break;
            case EInputFormat.PostalCode:
                secondAttribute = "data-rule-postalCodeCA = true";
                break;
            case EInputFormat.SocialInsuranceNumber:
                inputMask = "'alias': 'GCIS-social-insurance-number'";
                secondAttribute = "data-rule-minlength = 9";
                break;
            case EInputFormat.Currency:
                secondAttribute = "'alias': 'currency_en'";
                break;
        }

        if (!inputMask.Equals("")) { MergeAttribute(context.Attributes, "data-inputmask", inputMask); }
        if (!secondAttribute.Equals("")) { MergeAttribute(context.Attributes, secondAttribute, ""); }

    }
    
    bool MergeAttribute(IDictionary<string, string> attributes, string key, string value)
    {
        if (attributes.ContainsKey(key))
        {
            return false;
        }
        attributes.Add(key, value);
        return true;
    }

   public override bool IsValid(object value)
    {
        //Add more validation on submit...
        return true;
    }

}

视图模型中使用验证属性的属性:

[Display(Name = "Telephone number")]    
[InputFormat(EInputFormat.PhoneNumber)]
public string PhoneNumber { get; set; } 

当我将自定义验证属性与自定义标签助手一起使用时 IClientModelValidator 方法 AddValidation(ClientModelValidationContext context) 永远不会被调用 验证工作永远不会完成。

但是,当我不使用我的标签助手而只使用静态 HTML 时 所以:

<label class="control-label" asp-for="Phone"> @*works*@
   <span class="field-name">Phone</span`>`
</label>
<input class="form-control" asp-for="Phone">

验证属性方法确实运行....任何人都知道为什么属性类只适用于静态 html 而不是标签助手?

【问题讨论】:

    标签: asp.net-core model-view-controller


    【解决方案1】:

    验证属性方法确实运行....任何人都知道为什么 属性类仅适用于静态 html 而不是标签 帮手?

    很明显,text-box 不是官方定义的标签,所以需要将其创建为自定义标签助手,使其与input 标签相同。

    为了让text-boxasp-for 属性自动触发对您添加到PhoneNumberInputFormatAttribute 的验证,您创建的自定义标记助手需要继承自InputTagHelper

    首先,在您的项目中创建以下 MyCustomTagHelper 类:

    namespace MyProject.CustomTagHelper
    {
        [HtmlTargetElement("text-box", Attributes = "asp-for")]
        public class MyCustomTagHelper : InputTagHelper
        {
            public MyCustomTagHelper(IHtmlGenerator htmlGenerator) : base(htmlGenerator) { }
          
            public override void Process(TagHelperContext context, TagHelperOutput output)
            {
                base.Process(context, output);// the key point
                output.TagName = "input";
                output.Attributes.SetAttribute("class", "form-control");
            }
        }
    }
    

    然后,在 Views/_ViewImports.cshtml 文件中,添加以下代码(这里 MyProject 是您的项目名称):

    @addTagHelper *, MyProject
    

    使用文本框标签助手如下:

    <text-box asp-for="@Model.PhoneNumber"></text-box>   
    

    这是加载视图后text-box标签助手的渲染html

    <input type="text" data-inputmask="'alias': 'GCIS-phone-number'" data-rule-phoneus="true=&quot;&quot;" id="PhoneNumber" name="PhoneNumber" value="123432" class="form-control">
    

    调试过程如下:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2013-04-15
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多