如何在验证消息前面的验证摘要中获取错误字段的 ID

发布于 2024-10-21 05:33:41 字数 283 浏览 5 评论 0原文

有没有办法自定义validationSummary,以便它可以输出锚标记,其HREF是摘要中验证消息显示的字段的名称?这样,使用 jquery,我可以添加 onclick 事件,当在验证摘要上单击锚标记时,这些事件会聚焦于该字段。

这主要针对视障人士,这样当他们出现错误时,验证摘要会聚焦,他们会选择错误条目,带有字段标签的锚点标签会聚焦,屏幕阅读器会先读取锚点,然后读取消息,然后他们可以单击聚焦在错误的字段上。

名字 - 请输入您的名字。

谢谢。

Is there a way to customize the validationSummary so that it can output anchor tags who's HREF is the name of the field that the validation message in teh summary is displaying for? This way, using jquery, i can add onclick events that focus the field when the anchor tag is clicked on the validation summary.

This is primarely for visually impaired people, so that when they have errors, the validation summary focuses, they tab to an error entry, the anchor tag with the field label focuses and the screen reader reads the anchor then the message, then they can click on the anchor to focus on the erroneous field.

<a href="#First_Name">First Name</a> - Please enter your first name.

Thanks.

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

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

发布评论

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

评论(1

涫野音 2024-10-28 05:33:41

我认为框架内没有任何功能,因此您需要使用自定义扩展方法。例如:

    public static string AccessibleValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes)
    {
        // Nothing to do if there aren't any errors
        if (htmlHelper.ViewData.ModelState.IsValid)
        {
            return null;
        }

        string messageSpan;
        if (!String.IsNullOrEmpty(message))
        {
            TagBuilder spanTag = new TagBuilder("span");
            spanTag.MergeAttributes(htmlAttributes);
            spanTag.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName);
            spanTag.SetInnerText(message);
            messageSpan = spanTag.ToString(TagRenderMode.Normal) + Environment.NewLine;
        }
        else
        {
            messageSpan = null;
        }

        StringBuilder htmlSummary = new StringBuilder();
        TagBuilder unorderedList = new TagBuilder("ul");
        unorderedList.MergeAttributes(htmlAttributes);
        unorderedList.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName);

        foreach (string key in htmlHelper.ViewData.ModelState.Keys)
        {
            ModelState modelState = htmlHelper.ViewData.ModelState[key];
            foreach (ModelError modelError in modelState.Errors)
            {
                string errorText = htmlHelper.ValidationMessage(key);
                if (!String.IsNullOrEmpty(errorText))
                {
                    TagBuilder listItem = new TagBuilder("li");

                    TagBuilder aTag = new TagBuilder("a");
                    aTag.Attributes.Add("href", "#" + key);
                    aTag.InnerHtml = errorText;
                    listItem.InnerHtml = aTag.ToString(TagRenderMode.Normal);
                    htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
                }
            }
        }

        unorderedList.InnerHtml = htmlSummary.ToString();

        return messageSpan + unorderedList.ToString(TagRenderMode.Normal);
    }

这是使用框架内现有的扩展方法并更改插入列表中的标签。这是一个快速示例,在使用它之前需要考虑一些事情:

  • 这不会对错误消息进行编码,因为我已经使用了现有的 html.ValidationMessage。如果您需要对消息进行编码,那么您需要包含代码以提供默认消息和任何本地化要求。
  • 由于使用现有的 ValidationMessage 方法,锚点内有一个 span 标记。如果你想整理你的 HTML 那么应该替换它。
  • 这是常见重载中最复杂的一个。如果您想使用一些更简单的签名,例如 html.ValidationSummary(),那么您需要创建相关签名并调用所提供的方法。

I don't think that there is any functionality within the framework for this so you would need to use a custom extension method. For example:

    public static string AccessibleValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes)
    {
        // Nothing to do if there aren't any errors
        if (htmlHelper.ViewData.ModelState.IsValid)
        {
            return null;
        }

        string messageSpan;
        if (!String.IsNullOrEmpty(message))
        {
            TagBuilder spanTag = new TagBuilder("span");
            spanTag.MergeAttributes(htmlAttributes);
            spanTag.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName);
            spanTag.SetInnerText(message);
            messageSpan = spanTag.ToString(TagRenderMode.Normal) + Environment.NewLine;
        }
        else
        {
            messageSpan = null;
        }

        StringBuilder htmlSummary = new StringBuilder();
        TagBuilder unorderedList = new TagBuilder("ul");
        unorderedList.MergeAttributes(htmlAttributes);
        unorderedList.MergeAttribute("class", HtmlHelper.ValidationSummaryCssClassName);

        foreach (string key in htmlHelper.ViewData.ModelState.Keys)
        {
            ModelState modelState = htmlHelper.ViewData.ModelState[key];
            foreach (ModelError modelError in modelState.Errors)
            {
                string errorText = htmlHelper.ValidationMessage(key);
                if (!String.IsNullOrEmpty(errorText))
                {
                    TagBuilder listItem = new TagBuilder("li");

                    TagBuilder aTag = new TagBuilder("a");
                    aTag.Attributes.Add("href", "#" + key);
                    aTag.InnerHtml = errorText;
                    listItem.InnerHtml = aTag.ToString(TagRenderMode.Normal);
                    htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
                }
            }
        }

        unorderedList.InnerHtml = htmlSummary.ToString();

        return messageSpan + unorderedList.ToString(TagRenderMode.Normal);
    }

This is using the existing extension method from within the framework and changing the tag that is inserted into the list. This is a quick sample and there are some things to consider before using this:

  • This does not encode the error message as I have used the existing html.ValidationMessage. If you need to encode the message then you would need to include so code to provide default messages and any localization requirements.
  • Due to the use of the existing ValidationMessage method there is a span tag within the anchor. If you want to tidy your HTML then this should be replaced.
  • This is the most complicated of the usual overloads. If you want to use some of the simpler ones such as html.ValidationSummary() then you would need to create the relevant signatures and call to the method provided.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文