Try our conversational search powered by Generative AI!

XForms: too many validation summary entries for 1 field

Vote:
 

Hello.

In my xform, we have a field where the user writes his/her email address. We've set this to validate as an email address. The validation works, but when it prints out the "email address didn't pass validation" error message in the validation summary at the top, it prints out the same message 3 times, even though it is just 1 field that hasn't passed validation.

Does anyone know what this is?

#82834
Mar 20, 2014 10:36
Vote:
 

Can you give us the code from the view where the validation is made?

#82865
Mar 20, 2014 16:19
Vote:
 

Hi,

I had exactly the same issue after upgrading one of the sites from Epi 7 to 7.5.

I have used in view

@Html.ValidationSummary()

and after upgrade errors were there multiplied by 3 e.g. when there were 5 errors on one form validation summary showed there 15 errors (each of errors repeated 3 times)

As a solution I have created own extension method which filters the error messages:

		public static MvcHtmlString FilteredValidationSummary(this HtmlHelper html, ModelStateDictionary modelState)
		{
			List errors = new List();
			StringBuilder builder = new StringBuilder();

			builder.AppendLine(@"
"); builder.AppendLine("
    "); foreach (var state in modelState.Values) { foreach (var error in state.Errors) { if (!errors.Contains(error.ErrorMessage)) { errors.Add(error.ErrorMessage); builder.AppendFormat(@"
  • {0}
  • ", error.ErrorMessage); } } } builder.AppendLine("
"); builder.AppendLine("
"); if (errors.Count > 0) return new MvcHtmlString(builder.ToString()); else return null; }

and just callled that from the view:

@Html.FilteredValidationSummary(ViewContext.ViewData.ModelState)
#88595
Edited, Jul 18, 2014 8:26
Vote:
 

I've just run into the same issue and was about to do much the same as yourself Grzegorz, so thanks for saving me some legwork.

#91026
Sep 24, 2014 12:29
Vote:
 

Just a small update on code snippet :)

public static MvcHtmlString FilteredValidationSummary(this HtmlHelper html, ModelStateDictionary modelState)
{
    var errors = new List<string>();
    var builder = new StringBuilder();

    builder.AppendLine(@"<div class=""validation-summary-errors"">");
    builder.AppendLine("<ul>");
    foreach (var error in modelState.Values.SelectMany(state => state.Errors.Where(error => !errors.Contains(error.ErrorMessage))))
    {
        errors.Add(error.ErrorMessage);
        builder.AppendFormat(@"<li>{0}</li>", error.ErrorMessage);
    }

    builder.AppendLine("</ul>");
    builder.AppendLine("</div>");

    return errors.Count > 0 ? new MvcHtmlString(builder.ToString()) : null;
}
#109786
Oct 15, 2014 8:28
Vote:
 

This workaround works fine only if you distinguish the particular input fields by ToolTip in the xform definition. If you leave the ToolTip empty then all error messages will be the same, e.g. for required fileds the message is "Vaadittua kenttää ei ole määritetty". So if you have 2 mandatory inputs then the workaround solution above will display only one error message in the validation summary, which is wrong :( So definitely this whole issue should be reported as bug to EPiServer.

#114947
Jan 05, 2015 13:24
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.