diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index 0a15584962..06b9968f4a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -455,16 +455,10 @@ namespace Microsoft.AspNet.Mvc.Rendering string tag) { var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null; - - if (ViewData.ModelState.IsValid == true) + if (ViewData.ModelState.IsValid && (formContext == null || excludePropertyErrors)) { - if (formContext == null || - ViewContext.UnobtrusiveJavaScriptEnabled && - excludePropertyErrors) - { - // No client side validation/updates - return HtmlString.Empty; - } + // No client side validation/updates + return HtmlString.Empty; } string wrappedMessage; @@ -483,6 +477,8 @@ namespace Microsoft.AspNet.Mvc.Rendering wrappedMessage = null; } + // If excludePropertyErrors is true, describe any validation issue with the current model in a single item. + // Otherwise, list individual property errors. var htmlSummary = new StringBuilder(); var modelStates = ValidationHelpers.GetModelStateList(ViewData, excludePropertyErrors); @@ -514,7 +510,7 @@ namespace Microsoft.AspNet.Mvc.Rendering var divBuilder = new TagBuilder("div"); divBuilder.MergeAttributes(htmlAttributes); - if (ViewData.ModelState.IsValid == true) + if (ViewData.ModelState.IsValid) { divBuilder.AddCssClass(HtmlHelper.ValidationSummaryValidCssClassName); } @@ -525,23 +521,10 @@ namespace Microsoft.AspNet.Mvc.Rendering divBuilder.InnerHtml = wrappedMessage + unorderedList.ToString(TagRenderMode.Normal); - if (formContext != null) + if (formContext != null && !excludePropertyErrors) { - if (ViewContext.UnobtrusiveJavaScriptEnabled) - { - if (!excludePropertyErrors) - { - // Only put errors in the validation summary if they're supposed to be included there - divBuilder.MergeAttribute("data-valmsg-summary", "true"); - } - } - else - { - // client validation summaries need an ID - divBuilder.GenerateId("validationSummary", IdAttributeDotReplacement); - formContext.ValidationSummaryId = divBuilder.Attributes["id"]; - formContext.ReplaceValidationSummary = !excludePropertyErrors; - } + // Inform the client where to replace the list of property errors after validation. + divBuilder.MergeAttribute("data-valmsg-summary", "true"); } return divBuilder.ToHtmlString(TagRenderMode.Normal); @@ -631,13 +614,12 @@ namespace Microsoft.AspNet.Mvc.Rendering return GetValidationAttributes(name, metadata: null); } - // Only render attributes if unobtrusive client-side validation is enabled, and then only if we've - // never rendered validation for a field with this name in this form. Also, if there's no form context, - // then we can't render the attributes (we'd have no
to attach them to). + // Only render attributes if client-side validation is enabled, and then only if we've + // never rendered validation for a field with this name in this form. protected IDictionary GetValidationAttributes(string name, ModelMetadata metadata) { - var formContext = ViewContext.GetFormContextForClientValidation(); - if (!ViewContext.UnobtrusiveJavaScriptEnabled || formContext == null) + var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null; + if (formContext == null) { return null; } @@ -787,25 +769,9 @@ namespace Microsoft.AspNet.Mvc.Rendering // method is an explicit parameter, so it takes precedence over the htmlAttributes. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), replaceExisting: true); - var traditionalJavascriptEnabled = ViewContext.ClientValidationEnabled && - !ViewContext.UnobtrusiveJavaScriptEnabled; - if (traditionalJavascriptEnabled) - { - // TODO revive ViewContext.FormIdGenerator(), WebFx-199 - // forms must have an ID for client validation - var formName = "form" + new Guid().ToString(); - tagBuilder.GenerateId(formName, IdAttributeDotReplacement); - } - ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); - var theForm = CreateForm(); - if (traditionalJavascriptEnabled) - { - ViewContext.FormContext.FormId = tagBuilder.Attributes["id"]; - } - - return theForm; + return CreateForm(); } protected virtual HtmlString GenerateHidden( @@ -1279,8 +1245,7 @@ namespace Microsoft.AspNet.Mvc.Rendering throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "expression"); } - var formContext = ViewContext.GetFormContextForClientValidation(); - + var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null; if (!ViewData.ModelState.ContainsKey(modelName) && formContext == null) { return null; @@ -1327,18 +1292,11 @@ namespace Microsoft.AspNet.Mvc.Rendering if (formContext != null) { + builder.MergeAttribute("data-valmsg-for", modelName); + var replaceValidationMessageContents = string.IsNullOrEmpty(message); - - if (ViewContext.UnobtrusiveJavaScriptEnabled) - { - builder.MergeAttribute("data-valmsg-for", modelName); - builder.MergeAttribute("data-valmsg-replace", - replaceValidationMessageContents.ToString().ToLowerInvariant()); - } - - // TODO: (WebFX-217) Add support for Unobtrusive JS disabled - - // Modify the field metadata to add the validation message, - // Add the client validation id in the field metadata + builder.MergeAttribute("data-valmsg-replace", + replaceValidationMessageContents.ToString().ToLowerInvariant()); } return builder.ToHtmlString(TagRenderMode.Normal); diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/MvcForm.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/MvcForm.cs index 07e3630be5..12651bf5e6 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/MvcForm.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/MvcForm.cs @@ -35,8 +35,6 @@ namespace Microsoft.AspNet.Mvc.Rendering protected virtual void GenerateEndForm() { _viewContext.Writer.Write(""); - - // TODO revive viewContext.OutputClientValidation(), this requires GetJsonValidationMetadata(), GitHub #163 _viewContext.FormContext = null; } diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs b/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs index 7f3c47ca9d..25024e8049 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewContext.cs @@ -27,7 +27,6 @@ namespace Microsoft.AspNet.Mvc Writer = writer; _formContext = _defaultFormContext; - UnobtrusiveJavaScriptEnabled = true; ClientValidationEnabled = true; ValidationSummaryMessageElement = "span"; ValidationMessageElement = "span"; @@ -41,7 +40,6 @@ namespace Microsoft.AspNet.Mvc : base(viewContext) { _formContext = viewContext.FormContext; - UnobtrusiveJavaScriptEnabled = viewContext.UnobtrusiveJavaScriptEnabled; ClientValidationEnabled = viewContext.ClientValidationEnabled; ValidationSummaryMessageElement = viewContext.ValidationSummaryMessageElement; ValidationMessageElement = viewContext.ValidationMessageElement; @@ -64,8 +62,6 @@ namespace Microsoft.AspNet.Mvc } } - public bool UnobtrusiveJavaScriptEnabled { get; set; } - public bool ClientValidationEnabled { get; set; } /// diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs index 2a39b1df4a..0299832175 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs @@ -26,9 +26,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public string ErrorMessage { get; private set; } /// - /// Identifier of the . If client-side unobtrustive validation is - /// enabled, use this as part of the generated "data-val" attribute name. Must be - /// unique in the set of enabled validation rules. + /// Identifier of the . If client-side validation is enabled, default + /// validation attribute generator uses this as part of the generated "data-val" + /// attribute name. Must be unique in the set of enabled validation rules. /// public string ValidationType { get; private set; }