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

Commit b3d6569

Browse files
author
NTaylorMullen
committed
Add ValidationSummaryTagHelper.
#1251
1 parent af0ac42 commit b3d6569

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;div&gt; elements with <c>validation-summary</c> or
12+
/// <c>validation-model-errors-only</c> attributes.
13+
/// </summary>S
14+
[TagName("div")]
15+
[ContentBehavior(ContentBehavior.Append)]
16+
public class ValidationSummaryTagHelper : TagHelper
17+
{
18+
private static readonly bool DefaultModelErrorsOnlyBehavior = false;
19+
20+
[Activate]
21+
private ViewContext ViewContext { get; set; }
22+
23+
[Activate]
24+
private IHtmlGenerator Generator { get; set; }
25+
26+
/// <summary>
27+
/// If <c>true</c> appends a validation summary.
28+
/// </summary>
29+
/// <remarks>If <see cref="ValidationModelErrorsOnly"/> is provided this is assumed <c>true</c> unless
30+
/// specified <c>false</c>.</remarks>
31+
[HtmlAttributeName("validation-summary")]
32+
public bool? ValidationSummary { get; set; }
33+
34+
/// <summary>
35+
/// If <c>true</c>, display model-level errors only; otherwise display all errors. Defaults behavior is
36+
/// <c>false</c>.
37+
/// </summary>
38+
/// <remarks>
39+
/// If provided <see cref="ValidationSummary"/> is assumed <c>true</c> unless specified <c>false</c>.
40+
/// </remarks>
41+
[HtmlAttributeName("validation-model-errors-only")]
42+
public bool? ValidationModelErrorsOnly { get; set; }
43+
44+
/// <inheritdoc />
45+
public override void Process(TagHelperContext context, TagHelperOutput output)
46+
{
47+
// If the user specified validation-summary, use the provided value. If validation-summary was not
48+
// specified and validation-model-errors-only was, assume they want a validation summary.
49+
var generateValidationSummary = (ValidationSummary.HasValue && ValidationSummary.Value) ||
50+
(!ValidationSummary.HasValue && ValidationModelErrorsOnly.HasValue);
51+
52+
if (generateValidationSummary)
53+
{
54+
// If the user specified their validation-model-errors-only behavior, use it; otherwise revert to
55+
// the default behavior (false).
56+
var validationModelErrorsOnly = ValidationModelErrorsOnly.HasValue ?
57+
ValidationModelErrorsOnly.Value :
58+
DefaultModelErrorsOnlyBehavior;
59+
60+
var tagBuilder = Generator.GenerateValidationSummary(
61+
ViewContext,
62+
validationModelErrorsOnly,
63+
// We don't have access to the users message, it's implied by the content of the <div> tag.
64+
message: string.Empty,
65+
headerTag: output.TagName,
66+
htmlAttributes: null);
67+
68+
output.Merge(tagBuilder);
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)