Port of changes for ValidationMessage from MVC.

This commit is contained in:
sornaks 2014-04-15 14:42:27 -07:00
parent d63d998e95
commit 458c389aae
1 changed files with 35 additions and 0 deletions

View File

@ -55,5 +55,40 @@ namespace Microsoft.AspNet.Mvc.Rendering
return htmlHelper.ValidationSummary(excludePropertyErrors: false, message: message,
htmlAttributes: htmlAttributes);
}
private IHtmlString BuildValidationMessage(string name, string message, IDictionary<string, object> htmlAttributes)
{
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();
}
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);
}
}
}
}