Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 108a0ba

Browse files
author
NTaylorMullen
committed
Add ValidationMessage TagHelper.
#1250
1 parent 917d849 commit 108a0ba

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.Mvc.Rendering;
5+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
6+
using Microsoft.AspNet.Razor.TagHelpers;
7+
8+
namespace Microsoft.AspNet.Mvc.TagHelpers
9+
{
10+
/// <summary>
11+
/// <see cref="ITagHelper"/> implementation targeting &lt;span&gt; elements with <c>validation-for</c> attributes.
12+
/// </summary>
13+
[TagName("span")]
14+
[ContentBehavior(ContentBehavior.Modify)]
15+
public class ValidationMessageTagHelper : TagHelper
16+
{
17+
[Activate]
18+
private ViewContext ViewContext { get; set; }
19+
20+
[Activate]
21+
private IHtmlGenerator Generator { get; set; }
22+
23+
/// <summary>
24+
/// Name to be validated on the current model.
25+
/// </summary>
26+
[HtmlAttributeName("validation-for")]
27+
public ModelExpression For { get; set; }
28+
29+
/// <inheritdoc />
30+
public override void Process(TagHelperContext context, TagHelperOutput output)
31+
{
32+
if (For != null)
33+
{
34+
var tagBuilder = Generator.GenerateValidationMessage(ViewContext,
35+
For.Name,
36+
message: output.Content,
37+
tag: output.TagName,
38+
htmlAttributes: null);
39+
40+
output.Merge(tagBuilder);
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)