From de8706e4c5263a3afae90118564b4d07342a75b6 Mon Sep 17 00:00:00 2001 From: NTaylorMullen Date: Sat, 11 Oct 2014 23:10:54 -0700 Subject: [PATCH] Add ValidationMessage TagHelper. #1250 --- .../ValidationMessageTagHelper.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Microsoft.AspNet.Mvc.TagHelpers/ValidationMessageTagHelper.cs diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/ValidationMessageTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/ValidationMessageTagHelper.cs new file mode 100644 index 0000000000..a336c33c86 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/ValidationMessageTagHelper.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace Microsoft.AspNet.Mvc.TagHelpers +{ + /// + /// implementation targeting <span> elements with validation-for attributes. + /// + [TagName("span")] + [ContentBehavior(ContentBehavior.Modify)] + public class ValidationMessageTagHelper : TagHelper + { + [Activate] + private ViewContext ViewContext { get; set; } + + [Activate] + private IHtmlGenerator Generator { get; set; } + + /// + /// Name to be validated on the current model. + /// + [HtmlAttributeName("validation-for")] + public ModelExpression For { get; set; } + + /// + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if (For != null) + { + var tagBuilder = Generator.GenerateValidationMessage(ViewContext, + For.Name, + message: output.Content, + tag: output.TagName, + htmlAttributes: null); + + output.Merge(tagBuilder); + } + } + } +} \ No newline at end of file