正在从验证摘要控件中删除重复的错误消息

本文关键字:删除 消息 错误 控件 验证 | 更新日期: 2023-09-27 18:30:07

我遇到的情况是,我有多个验证器在一个页面上显示相同的错误消息,当验证页面时,这些重复项会显示在验证摘要中。

我想从验证摘要服务器端删除这些重复项。

这是我想做的一些伪代码。

validationSummery.ErrorMessages = validationSummery.ErrorMessages.DistinctBy(x=>x.ErrorText);

查看验证控件后,似乎无法访问它显示的消息。

我可以在验证摘要到达之前迭代所有无效的页面验证器,并只将每个消息类型中的一个设置为有效,但这样我就不会在每个控件旁边获得错误消息。

有人知道怎么做吗?

正在从验证摘要控件中删除重复的错误消息

这并不漂亮,但通过使用dotPeek获取ValidationSummary的源代码以及一些反射,我创建了一个UniqueMessageValidationSummary控件。

 /// <summary>
    /// Extended version of Validation Summary which overrides OnRender and re-implements get error
    /// messages method to ensure the control only renders unique error messages.
    /// 
    /// Utilizes .NET code cleaned from .Peek and reflection to access subclass     
    /// </summary>
    public class UniqueMessageValidationSummary : ValidationSummary 
    {
        internal string[] GetErrorMessages(out bool inError)
        {
            var strArray = (string[])null;
            inError = false;
            var length = 0;
            var validators = Page.GetValidators(ValidationGroup);
            for (var index = 0; index < validators.Count; ++index)
            {
                var validator = validators[index];
                if (!validator.IsValid)
                {
                    inError = true;
                    if (validator.ErrorMessage.Length != 0)
                        ++length;
                }
            }
            if (length != 0)
            {
                strArray = new string[length];
                var index1 = 0;
                for (var index2 = 0; index2 < validators.Count; ++index2)
                {
                    var validator = validators[index2];
                    if (!validator.IsValid && !string.IsNullOrEmpty(validator.ErrorMessage))
                    {
                        strArray[index1] = string.Copy(validator.ErrorMessage);
                        ++index1;
                    }
                }
            }
            var uniqueErrors = new List<string>();
            if (strArray != null)
            {
               var objRegExp = new Regex("<(.|'n)+?>");       
                foreach (var error in strArray)
                {
                    if (uniqueErrors.All(x => objRegExp.Replace(error, string.Empty) != objRegExp.Replace(x, String.Empty)))
                    {
                        uniqueErrors.Add(error);
                    }
                }
            }
            return uniqueErrors.ToArray();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            var renderUplevelCopy = true;
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var baseType = GetType().BaseType;
            if (baseType != null)
            {
                var field = baseType.GetField("renderUplevel", flags);
                if (field != null)
                    renderUplevelCopy = (bool)field.GetValue(this);
            }  
            string[] strArray;
            bool flag1;
            if (DesignMode)
            {
                flag1 = true;
                renderUplevelCopy = false;
                strArray = new[]
                    {
                      "ValSummary_error_message_1",
                      "ValSummary_error_message_2"
                    };
            }
            else
            {
                if (!Enabled)
                    return;
                bool inError;
                strArray = GetErrorMessages(out inError);
                flag1 = ShowSummary && inError;
                if (!flag1 && renderUplevelCopy)
                    Style["display"] = "none";
            }
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            var flag2 = renderUplevelCopy || flag1;
            if (flag2)
                RenderBeginTag(writer);
            if (flag1)
            {
                string text1;
                string str1;
                string str2;
                string text2;
                string text3;
                switch (DisplayMode)
                {
                    case ValidationSummaryDisplayMode.List:
                        text1 = "b";
                        str1 = string.Empty;
                        str2 = string.Empty;
                        text2 = "b";
                        text3 = string.Empty;
                        break;
                    case ValidationSummaryDisplayMode.SingleParagraph:
                        text1 = " ";
                        str1 = string.Empty;
                        str2 = string.Empty;
                        text2 = " ";
                        text3 = "b";
                        break;
                    default:
                        text1 = string.Empty;
                        str1 = "<ul>";
                        str2 = "<li>";
                        text2 = "</li>";
                        text3 = "</ul>";
                        break;
                }
                if (HeaderText.Length > 0)
                {
                    writer.Write(HeaderText);
                    WriteBreakIfPresent(writer, text1);
                }
                if (strArray != null)
                {
                    writer.Write(str1);
                    foreach (var t in strArray)
                    {
                        writer.Write(str2);
                        writer.Write(t);
                        WriteBreakIfPresent(writer, text2);
                    }
                    WriteBreakIfPresent(writer, text3);
                }
            }
            if (!flag2)
                return;
            RenderEndTag(writer);
        }
        private static void WriteBreakIfPresent(HtmlTextWriter writer, string text)
        {
            if (text == "b")
            {
                writer.WriteBreak();
            }
            else
                writer.Write(text);
        }
    }