Fix #620, Remove legacy / non-unobtrusive client side validation

- remove `ViewContext.UnobtrusiveJavaScriptEnabled` property and all references
- avoid `ViewContext.GetFormContextForClientValidation()` calls since
  ternary expression is more explicit and we were inconsistent
- improve `ValidationMessage()` comments
- don't treat `ModelState.IsValid` as if it were still nullable
This commit is contained in:
dougbu 2014-07-25 16:28:52 -07:00
parent b0d52f73fd
commit b5dcc9895d
4 changed files with 22 additions and 70 deletions

View File

@ -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 <form> 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<string, object> 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);

View File

@ -35,8 +35,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
protected virtual void GenerateEndForm()
{
_viewContext.Writer.Write("</form>");
// TODO revive viewContext.OutputClientValidation(), this requires GetJsonValidationMetadata(), GitHub #163
_viewContext.FormContext = null;
}

View File

@ -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; }
/// <summary>

View File

@ -26,9 +26,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public string ErrorMessage { get; private set; }
/// <summary>
/// Identifier of the <see cref="ModelClientValidationRule"/>. If client-side unobtrustive validation is
/// enabled, use this <see langref="string"/> as part of the generated "data-val" attribute name. Must be
/// unique in the set of enabled validation rules.
/// Identifier of the <see cref="ModelClientValidationRule"/>. If client-side validation is enabled, default
/// validation attribute generator uses this <see langref="string"/> as part of the generated "data-val"
/// attribute name. Must be unique in the set of enabled validation rules.
/// </summary>
public string ValidationType { get; private set; }