Porting the ValidationMessage changes to suit WebFX requirements.

This commit is contained in:
sornaks 2014-04-15 14:52:32 -07:00
parent 458c389aae
commit f17d444b8a
3 changed files with 90 additions and 39 deletions

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// Initializes a new instance of the <see cref="HtmlHelper"/> class.
/// </summary>
public HtmlHelper(
[NotNull] IViewEngine viewEngine,
[NotNull] IViewEngine viewEngine,
[NotNull] IModelMetadataProvider metadataProvider,
[NotNull] IUrlHelper urlHelper,
[NotNull] AntiForgery antiForgeryInstance)
@ -98,13 +98,13 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <inheritdoc />
public HtmlString ActionLink(
[NotNull] string linkText,
string actionName,
string controllerName,
string protocol,
string hostname,
string fragment,
object routeValues,
[NotNull] string linkText,
string actionName,
string controllerName,
string protocol,
string hostname,
string fragment,
object routeValues,
object htmlAttributes)
{
var url = _urlHelper.Action(actionName, controllerName, routeValues);
@ -508,6 +508,46 @@ namespace Microsoft.AspNet.Mvc.Rendering
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>
/// Returns the HTTP method that handles form input (GET or POST) as a string.
/// </summary>

View File

@ -56,39 +56,40 @@ namespace Microsoft.AspNet.Mvc.Rendering
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];
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();
}
return ValidationMessage(htmlHelper, modelName, message: null, htmlAttributes: null);
}
TagBuilder tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
tagBuilder.MergeAttributes(htmlAttributes);
if (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);
}
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
string modelName, string message)
{
return ValidationMessage(htmlHelper, modelName, message, htmlAttributes: null);
}
public static HtmlString ValidationMessage<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper,
string modelName, object htmlAttributes)
{
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);
}
}
}

View File

@ -223,6 +223,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
HtmlString TextBoxFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, string format,
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>
/// Returns the model value for the given expression <paramref name="expression"/>.
/// </summary>