From 94db3c392a61a7d26a84a2da6bc583a39ff404c2 Mon Sep 17 00:00:00 2001 From: dougbu Date: Fri, 28 Mar 2014 17:01:23 -0700 Subject: [PATCH] Add `FormContext` to `ViewContext` - start with `FormContext` and classes it needs from legacy world - FYI `ModelClientValidationRule` came from Web Pages; the rest from MVC Cleanup and make files compile in new world - remove `FormContext.GetJsonValidationMetadata` method; will file an issue to revive this using Json.NET - don't store `FormContext` in the `HttpContext`, no longer have child actions - do `null` checks in setters, not getters (minor perf improvement) - fix namespaces and usings - wrap long lines; use `[NotNull]`; no copyright notice - use explicit comparers for dictionaries - add XML comment for odd `ModelClientValidationRule.ValidationType` property - Collection -> List --- .../Validation/FieldValidationMetadata.cs | 27 ++++++++ .../Validation/ModelClientValidationRule.cs | 30 +++++++++ .../View/FormContext.cs | 61 +++++++++++++++++++ .../View/ViewContext.cs | 24 +++++++- 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/Validation/FieldValidationMetadata.cs create mode 100644 src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/View/FormContext.cs diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/FieldValidationMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/FieldValidationMetadata.cs new file mode 100644 index 0000000000..367105f43a --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/FieldValidationMetadata.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Microsoft.AspNet.Mvc.ModelBinding +{ + public class FieldValidationMetadata + { + private readonly List _validationRules = + new List(); + private string _fieldName = string.Empty; + + public string FieldName + { + get { return _fieldName; } + set { _fieldName = value ?? string.Empty; } + } + + public bool ReplaceValidationMessageContents { get; set; } + + public string ValidationMessageId { get; set; } + + public IList ValidationRules + { + get { return _validationRules; } + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs new file mode 100644 index 0000000000..523964a451 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Validation/ModelClientValidationRule.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc.ModelBinding +{ + public class ModelClientValidationRule + { + private readonly Dictionary _validationParameters = + new Dictionary(StringComparer.Ordinal); + private string _validationType = string.Empty; + + public string ErrorMessage { get; set; } + + public IDictionary ValidationParameters + { + get { return _validationParameters; } + } + + /// + /// 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. + /// + public string ValidationType + { + get { return _validationType; } + set { _validationType = value ?? string.Empty; } + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/FormContext.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/FormContext.cs new file mode 100644 index 0000000000..52318ed08f --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/FormContext.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Mvc.ModelBinding; + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public class FormContext + { + private readonly Dictionary _fieldValidators = + new Dictionary(StringComparer.Ordinal); + private readonly Dictionary _renderedFields = + new Dictionary(StringComparer.Ordinal); + + public IDictionary FieldValidators + { + get { return _fieldValidators; } + } + + public string FormId { get; set; } + + public bool ReplaceValidationSummary { get; set; } + + public string ValidationSummaryId { get; set; } + + public FieldValidationMetadata GetValidationMetadataForField([NotNull] string fieldName) + { + return GetValidationMetadataForField(fieldName, createIfNotFound: false); + } + + public FieldValidationMetadata GetValidationMetadataForField([NotNull] string fieldName, bool createIfNotFound) + { + FieldValidationMetadata metadata; + if (!FieldValidators.TryGetValue(fieldName, out metadata)) + { + if (createIfNotFound) + { + metadata = new FieldValidationMetadata() + { + FieldName = fieldName + }; + FieldValidators[fieldName] = metadata; + } + } + + return metadata; + } + + public bool RenderedField([NotNull] string fieldName) + { + bool result; + _renderedFields.TryGetValue(fieldName, out result); + + return result; + } + + public void RenderedField([NotNull] string fieldName, bool value) + { + _renderedFields[fieldName] = value; + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs index 2459650222..9124a2335c 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using Microsoft.AspNet.Abstractions; @@ -9,15 +9,35 @@ namespace Microsoft.AspNet.Mvc.Rendering { private DynamicViewData _viewBag; - public ViewContext(IServiceProvider serviceProvider, HttpContext httpContext, IDictionary viewEngineContext) + // We need a default FormContext if the user uses html
instead of an MvcForm + private readonly FormContext _defaultFormContext = new FormContext(); + + private FormContext _formContext; + + public ViewContext(IServiceProvider serviceProvider, HttpContext httpContext, + IDictionary viewEngineContext) { ServiceProvider = serviceProvider; HttpContext = httpContext; ViewEngineContext = viewEngineContext; + _formContext = _defaultFormContext; } public IViewComponentHelper Component { get; set; } + public virtual FormContext FormContext + { + get + { + return _formContext; + } + set + { + // Never return a null form context, this is important for validation purposes. + _formContext = value ?? _defaultFormContext; + } + } + public HttpContext HttpContext { get; private set; } public IServiceProvider ServiceProvider { get; private set; }