This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ValidationSummaryTagHelper.cs
112 lines (99 loc) · 3.98 KB
/
ValidationSummaryTagHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Microsoft.AspNetCore.Mvc.TagHelpers
{
/// <summary>
/// <see cref="ITagHelper"/> implementation targeting any HTML element with an <c>asp-validation-summary</c>
/// attribute.
/// </summary>
[HtmlTargetElement("div", Attributes = ValidationSummaryAttributeName)]
public class ValidationSummaryTagHelper : TagHelper
{
private const string ValidationSummaryAttributeName = "asp-validation-summary";
private ValidationSummary _validationSummary;
/// <summary>
/// Creates a new <see cref="ValidationSummaryTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public ValidationSummaryTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}
/// <inheritdoc />
public override int Order => -1000;
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
[HtmlAttributeNotBound]
protected IHtmlGenerator Generator { get; }
/// <summary>
/// If <see cref="ValidationSummary.All"/> or <see cref="ValidationSummary.ModelOnly"/>, appends a validation
/// summary. Otherwise (<see cref="ValidationSummary.None"/>, the default), this tag helper does nothing.
/// </summary>
/// <exception cref="ArgumentException">
/// Thrown if setter is called with an undefined <see cref="ValidationSummary"/> value e.g.
/// <c>(ValidationSummary)23</c>.
/// </exception>
[HtmlAttributeName(ValidationSummaryAttributeName)]
public ValidationSummary ValidationSummary
{
get => _validationSummary;
set
{
switch (value)
{
case ValidationSummary.All:
case ValidationSummary.ModelOnly:
case ValidationSummary.None:
_validationSummary = value;
break;
default:
throw new ArgumentException(
message: Resources.FormatInvalidEnumArgument(
nameof(value),
value,
typeof(ValidationSummary).FullName),
paramName: nameof(value));
}
}
}
/// <inheritdoc />
/// <remarks>Does nothing if <see cref="ValidationSummary"/> is <see cref="ValidationSummary.None"/>.</remarks>
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
if (ValidationSummary == ValidationSummary.None)
{
return;
}
var tagBuilder = Generator.GenerateValidationSummary(
ViewContext,
excludePropertyErrors: ValidationSummary == ValidationSummary.ModelOnly,
message: null,
headerTag: null,
htmlAttributes: null);
if (tagBuilder == null)
{
// The generator determined no element was necessary.
output.SuppressOutput();
return;
}
output.MergeAttributes(tagBuilder);
if (tagBuilder.HasInnerHtml)
{
output.PostContent.AppendHtml(tagBuilder.InnerHtml);
}
}
}
}