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
This commit is contained in:
parent
54c73e4227
commit
94db3c392a
|
|
@ -0,0 +1,27 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
{
|
||||||
|
public class FieldValidationMetadata
|
||||||
|
{
|
||||||
|
private readonly List<ModelClientValidationRule> _validationRules =
|
||||||
|
new List<ModelClientValidationRule>();
|
||||||
|
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<ModelClientValidationRule> ValidationRules
|
||||||
|
{
|
||||||
|
get { return _validationRules; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
{
|
||||||
|
public class ModelClientValidationRule
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, object> _validationParameters =
|
||||||
|
new Dictionary<string, object>(StringComparer.Ordinal);
|
||||||
|
private string _validationType = string.Empty;
|
||||||
|
|
||||||
|
public string ErrorMessage { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<string, object> ValidationParameters
|
||||||
|
{
|
||||||
|
get { return _validationParameters; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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.
|
||||||
|
/// </summary>
|
||||||
|
public string ValidationType
|
||||||
|
{
|
||||||
|
get { return _validationType; }
|
||||||
|
set { _validationType = value ?? string.Empty; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<string, FieldValidationMetadata> _fieldValidators =
|
||||||
|
new Dictionary<string, FieldValidationMetadata>(StringComparer.Ordinal);
|
||||||
|
private readonly Dictionary<string, bool> _renderedFields =
|
||||||
|
new Dictionary<string, bool>(StringComparer.Ordinal);
|
||||||
|
|
||||||
|
public IDictionary<string, FieldValidationMetadata> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
|
|
@ -9,15 +9,35 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
{
|
{
|
||||||
private DynamicViewData _viewBag;
|
private DynamicViewData _viewBag;
|
||||||
|
|
||||||
public ViewContext(IServiceProvider serviceProvider, HttpContext httpContext, IDictionary<string, object> viewEngineContext)
|
// We need a default FormContext if the user uses html <form> instead of an MvcForm
|
||||||
|
private readonly FormContext _defaultFormContext = new FormContext();
|
||||||
|
|
||||||
|
private FormContext _formContext;
|
||||||
|
|
||||||
|
public ViewContext(IServiceProvider serviceProvider, HttpContext httpContext,
|
||||||
|
IDictionary<string, object> viewEngineContext)
|
||||||
{
|
{
|
||||||
ServiceProvider = serviceProvider;
|
ServiceProvider = serviceProvider;
|
||||||
HttpContext = httpContext;
|
HttpContext = httpContext;
|
||||||
ViewEngineContext = viewEngineContext;
|
ViewEngineContext = viewEngineContext;
|
||||||
|
_formContext = _defaultFormContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IViewComponentHelper Component { get; set; }
|
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 HttpContext HttpContext { get; private set; }
|
||||||
|
|
||||||
public IServiceProvider ServiceProvider { get; private set; }
|
public IServiceProvider ServiceProvider { get; private set; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue