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

Commit ba111a4

Browse files
author
NTaylorMullen
committed
Add ValidationSummaryTagHelper.
- Tested ValidationSummaryTagHelper behavior. - Updated sample to utilize new ValidationSummaryTagHelper format. #1251
1 parent 0d4d7c6 commit ba111a4

File tree

8 files changed

+418
-4
lines changed

8 files changed

+418
-4
lines changed

samples/TagHelperSample.Web/Views/Home/Create.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
<div class="form-horizontal">
1111
@* validation summary tag helper will target just <div/> elements and append the list of errors *@
1212
@* - i.e. this helper, like <select/> helper has ContentBehavior.Append *@
13-
@* validation-model-errors-only="true" implies validation-summary="true" *@
14-
@* helper does nothing if model is valid and (client-side validation is disabled or validation-model-errors-only="true") *@
13+
@* helper does nothing if model is valid and (client-side validation is disabled or validation-summary="ModelOnly") *@
1514
@* don't need a bound attribute to match Html.ValidationSummary()'s headerTag parameter; users wrap message as they wish *@
1615
@* initially at least, will not remove the <div/> if list isn't generated *@
1716
@* - should helper remove the <div/> if list isn't generated? *@
1817
@* - (Html.ValidationSummary returns empty string despite non-empty message parameter) *@
19-
<div validation-summary="true" validation-model-errors-only="true">
18+
@* Acceptable values are: "None", "ModelOnly" and "All" *@
19+
<div validation-summary="ModelOnly">
2020
<span style="color:red">This is my message</span>
2121
</div>
2222

samples/TagHelperSample.Web/Views/Home/Edit.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<form>
88
<div class="form-horizontal">
9-
<div validation-summary="true"/>
9+
<div validation-summary="All"/>
1010
<input type="hidden" for="Id" />
1111

1212
<div class="form-group">
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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 System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.TagHelpers.Test")]

src/Microsoft.AspNet.Mvc.TagHelpers/Properties/Resources.Designer.cs

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.AspNet.Mvc.TagHelpers/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,7 @@
126126
<data name="FormTagHelper_CannotDetermineAction" xml:space="preserve">
127127
<value>Cannot determine an {1} for {0}. A {0} with a URL-based {1} must not have attributes starting with {3} or a {2} attribute.</value>
128128
</data>
129+
<data name="ValidationSummaryTagHelper_InvalidValidationSummaryValue" xml:space="preserve">
130+
<value>Cannot parse '{1}' value '{2}' for {0}. Acceptable values are '{3}', '{4}' and '{5}'.</value>
131+
</data>
129132
</root>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
namespace Microsoft.AspNet.Mvc.TagHelpers
5+
{
6+
/// <summary>
7+
/// Acceptable validation summary rendering modes.
8+
/// </summary>
9+
public enum ValidationSummary
10+
{
11+
/// <summary>
12+
/// No validation summary.
13+
/// </summary>
14+
None,
15+
16+
/// <summary>
17+
/// Validation summary with model-level errors only (excludes all property errors).
18+
/// </summary>
19+
ModelOnly,
20+
21+
/// <summary>
22+
/// Validation summary with all errors.
23+
/// </summary>
24+
All
25+
}
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 System;
5+
using Microsoft.AspNet.Mvc.Rendering;
6+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
7+
using Microsoft.AspNet.Razor.TagHelpers;
8+
9+
namespace Microsoft.AspNet.Mvc.TagHelpers
10+
{
11+
/// <summary>
12+
/// <see cref="ITagHelper"/> implementation targeting &lt;div&gt; elements with a <c>validation-summary</c>
13+
/// attribute.
14+
/// </summary>
15+
[TagName("div")]
16+
[ContentBehavior(ContentBehavior.Append)]
17+
public class ValidationSummaryTagHelper : TagHelper
18+
{
19+
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
20+
[Activate]
21+
protected internal ViewContext ViewContext { get; set; }
22+
23+
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
24+
[Activate]
25+
protected internal IHtmlGenerator Generator { get; set; }
26+
27+
// TODO: Change to ValidationSummary enum once https://github.com/aspnet/Razor/issues/196 has been completed.
28+
/// <summary>
29+
/// If <c>All</c> or <c>ModelOnly</c>, appends a validation summary. Acceptable values are defined by the
30+
/// <see cref="ValidationSummary"/> enum.
31+
/// </summary>
32+
[HtmlAttributeName("validation-summary")]
33+
public string ValidationSummaryValue { get; set; }
34+
35+
/// <inheritdoc />
36+
/// Does nothing if <see cref="ValidationSummaryValue"/> is <c>null</c>, empty or "None".
37+
public override void Process(TagHelperContext context, TagHelperOutput output)
38+
{
39+
if (!string.IsNullOrEmpty(ValidationSummaryValue))
40+
{
41+
ValidationSummary validationSummaryValue;
42+
if (!Enum.TryParse(ValidationSummaryValue, ignoreCase: true, result: out validationSummaryValue))
43+
{
44+
throw new InvalidOperationException(
45+
Resources.FormatValidationSummaryTagHelper_InvalidValidationSummaryValue(
46+
"<div>",
47+
"validation-summary",
48+
ValidationSummaryValue,
49+
ValidationSummary.All,
50+
ValidationSummary.ModelOnly,
51+
ValidationSummary.None));
52+
}
53+
else if (validationSummaryValue == ValidationSummary.None)
54+
{
55+
return;
56+
}
57+
58+
var validationModelErrorsOnly = validationSummaryValue == ValidationSummary.ModelOnly;
59+
var tagBuilder = Generator.GenerateValidationSummary(
60+
ViewContext,
61+
excludePropertyErrors: validationModelErrorsOnly,
62+
message: null,
63+
headerTag: null,
64+
htmlAttributes: null);
65+
66+
if (tagBuilder != null)
67+
{
68+
output.MergeAttributes(tagBuilder);
69+
output.Content += tagBuilder.InnerHtml;
70+
}
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)