Porting the ValidationMessage changes to suit WebFX requirements.
This commit is contained in:
parent
458c389aae
commit
f17d444b8a
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
/// Initializes a new instance of the <see cref="HtmlHelper"/> class.
|
/// Initializes a new instance of the <see cref="HtmlHelper"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HtmlHelper(
|
public HtmlHelper(
|
||||||
[NotNull] IViewEngine viewEngine,
|
[NotNull] IViewEngine viewEngine,
|
||||||
[NotNull] IModelMetadataProvider metadataProvider,
|
[NotNull] IModelMetadataProvider metadataProvider,
|
||||||
[NotNull] IUrlHelper urlHelper,
|
[NotNull] IUrlHelper urlHelper,
|
||||||
[NotNull] AntiForgery antiForgeryInstance)
|
[NotNull] AntiForgery antiForgeryInstance)
|
||||||
|
|
@ -98,13 +98,13 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public HtmlString ActionLink(
|
public HtmlString ActionLink(
|
||||||
[NotNull] string linkText,
|
[NotNull] string linkText,
|
||||||
string actionName,
|
string actionName,
|
||||||
string controllerName,
|
string controllerName,
|
||||||
string protocol,
|
string protocol,
|
||||||
string hostname,
|
string hostname,
|
||||||
string fragment,
|
string fragment,
|
||||||
object routeValues,
|
object routeValues,
|
||||||
object htmlAttributes)
|
object htmlAttributes)
|
||||||
{
|
{
|
||||||
var url = _urlHelper.Action(actionName, controllerName, routeValues);
|
var url = _urlHelper.Action(actionName, controllerName, routeValues);
|
||||||
|
|
@ -508,6 +508,46 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return divBuilder.ToHtmlString(TagRenderMode.Normal);
|
return divBuilder.ToHtmlString(TagRenderMode.Normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HtmlString ValidationMessage(string name, string message, object htmlAttributes)
|
||||||
|
{
|
||||||
|
ModelState modelState;
|
||||||
|
ViewData.ModelState.TryGetValue(name, out modelState);
|
||||||
|
|
||||||
|
ModelErrorCollection errors = null;
|
||||||
|
if (modelState != null)
|
||||||
|
{
|
||||||
|
errors = modelState.Errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasError = errors != null && errors.Any();
|
||||||
|
if (!hasError && !ViewContext.UnobtrusiveJavaScriptEnabled)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string error = null;
|
||||||
|
if (hasError)
|
||||||
|
{
|
||||||
|
error = message ?? errors.First().ErrorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
|
||||||
|
tagBuilder.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes));
|
||||||
|
|
||||||
|
if (ViewContext.UnobtrusiveJavaScriptEnabled)
|
||||||
|
{
|
||||||
|
bool replaceValidationMessageContents = string.IsNullOrEmpty(message);
|
||||||
|
tagBuilder.MergeAttribute("data-valmsg-for", name);
|
||||||
|
tagBuilder.MergeAttribute("data-valmsg-replace",
|
||||||
|
replaceValidationMessageContents.ToString().ToLowerInvariant());
|
||||||
|
}
|
||||||
|
|
||||||
|
tagBuilder.AddCssClass(hasError ? ValidationMessageCssClassName : ValidationMessageValidCssClassName);
|
||||||
|
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the HTTP method that handles form input (GET or POST) as a string.
|
/// Returns the HTTP method that handles form input (GET or POST) as a string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -56,39 +56,40 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
htmlAttributes: htmlAttributes);
|
htmlAttributes: htmlAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IHtmlString BuildValidationMessage(string name, string message, IDictionary<string, object> htmlAttributes)
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
|
string modelName)
|
||||||
{
|
{
|
||||||
var modelState = ModelState[name];
|
return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: null);
|
||||||
IEnumerable<string> errors = null;
|
}
|
||||||
if (modelState != null)
|
|
||||||
{
|
|
||||||
errors = modelState.Errors;
|
|
||||||
}
|
|
||||||
bool hasError = errors != null && errors.Any();
|
|
||||||
if (!hasError && !UnobtrusiveJavaScriptEnabled)
|
|
||||||
{
|
|
||||||
// If unobtrusive validation is enabled, we need to generate an empty span with the "val-for" attribute"
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string error = null;
|
|
||||||
if (hasError)
|
|
||||||
{
|
|
||||||
error = message ?? errors.First();
|
|
||||||
}
|
|
||||||
|
|
||||||
TagBuilder tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
tagBuilder.MergeAttributes(htmlAttributes);
|
string modelName, string message)
|
||||||
if (UnobtrusiveJavaScriptEnabled)
|
{
|
||||||
{
|
return ValidationMessage(htmlHelper, modelName, message, htmlAttributes: null);
|
||||||
bool replaceValidationMessageContents = String.IsNullOrEmpty(message);
|
}
|
||||||
tagBuilder.MergeAttribute("data-valmsg-for", name);
|
|
||||||
tagBuilder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
}
|
string modelName, object htmlAttributes)
|
||||||
tagBuilder.AddCssClass(hasError ? ValidationMessageCssClassName : ValidationMessageValidCssClassName);
|
{
|
||||||
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
|
return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: htmlAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
|
string modelName, IDictionary<string, object> htmlAttributes)
|
||||||
|
{
|
||||||
|
return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: htmlAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
|
string modelName, string message, IDictionary<string, object> htmlAttributes)
|
||||||
|
{
|
||||||
|
return htmlHelper.ValidationMessage(modelName, message, htmlAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||||
|
string modelName, string message, object htmlAttributes)
|
||||||
|
{
|
||||||
|
return htmlHelper.ValidationMessage(modelName, message, htmlAttributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -223,6 +223,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
HtmlString TextBoxFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, string format,
|
HtmlString TextBoxFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, string format,
|
||||||
IDictionary<string, object> htmlAttributes);
|
IDictionary<string, object> htmlAttributes);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the validation message if an error exists in the <see cref="ModelStateDictionary"/> object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="modelName">The name of the property that is being validated.</param>
|
||||||
|
/// <param name="message">The message to be displayed if the specified field contains an error.</param>
|
||||||
|
/// <param name="htmlAttributes">Dictionary that contains the HTML attributes which should
|
||||||
|
/// be applied on the element</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
HtmlString ValidationMessage(string modelName, string message, object htmlAttributes);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the model value for the given expression <paramref name="expression"/>.
|
/// Returns the model value for the given expression <paramref name="expression"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue