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

Commit f617e67

Browse files
author
N. Taylor Mullen
committed
Added Enum to replace nullable bools.
- Also fixed up and added some tests
1 parent 71f0a3e commit f617e67

File tree

5 files changed

+103
-90
lines changed

5 files changed

+103
-90
lines changed

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 determine {0}. Acceptable values are '{1}', '{2}' and '{3}'.</value>
131+
</data>
129132
</root>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
// TODO: Make public once https://github.com/aspnet/Razor/issues/196 has been completed.
7+
internal enum ValidationSummary
8+
{
9+
None,
10+
All,
11+
ModelOnly
12+
}
13+
}

src/Microsoft.AspNet.Mvc.TagHelpers/ValidationSummaryTagHelper.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNet.Mvc.Rendering;
56
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
67
using Microsoft.AspNet.Razor.TagHelpers;
@@ -15,47 +16,41 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
1516
[ContentBehavior(ContentBehavior.Append)]
1617
public class ValidationSummaryTagHelper : TagHelper
1718
{
18-
private static readonly bool DefaultModelErrorsOnly = false;
19-
2019
[Activate]
2120
private ViewContext ViewContext { get; set; }
2221

2322
[Activate]
2423
private IHtmlGenerator Generator { get; set; }
2524

25+
// TODO: Change to ValidationSummary enum once https://github.com/aspnet/Razor/issues/196 has been completed.
2626
/// <summary>
27-
/// If <c>true</c> appends a validation summary.
27+
/// Acceptable values are <c>None</c>, <c>All</c> and <c>ModelOnly</c>.
2828
/// </summary>
29-
/// <remarks>Defaults to <c>false</c> unless <see cref="ValidationModelErrorsOnly"/> is <c>true</c> in which
30-
/// case this also defaults to <c>true</c></remarks>
29+
/// <remarks>If <c>ModelOnly</c> will display model-level errors only.</remarks>
3130
[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 to <c>false</c>.
36-
/// </summary>
37-
/// <remarks>
38-
/// Defaults <see cref="ValidationSummary"/> to <c>true</c>.
39-
/// </remarks>
40-
[HtmlAttributeName("validation-model-errors-only")]
41-
public bool? ValidationModelErrorsOnly { get; set; }
31+
public string ValidationSummaryValue { get; set; }
4232

4333
/// <inheritdoc />
4434
public override void Process(TagHelperContext context, TagHelperOutput output)
4535
{
46-
// If the user specified validation-summary, use the provided value. If validation-summary was not
47-
// specified and validation-model-errors-only was, assume they want a validation summary.
48-
var generateValidationSummary = (ValidationSummary.HasValue && ValidationSummary.Value) ||
49-
(!ValidationSummary.HasValue && ValidationModelErrorsOnly.HasValue);
50-
51-
if (generateValidationSummary)
36+
if (ValidationSummaryValue != null)
5237
{
53-
// If the user specified their validation-model-errors-only behavior, use it; otherwise revert to
54-
// the default behavior (false).
55-
var validationModelErrorsOnly = ValidationModelErrorsOnly.HasValue ?
56-
ValidationModelErrorsOnly.Value :
57-
DefaultModelErrorsOnly;
38+
ValidationSummary validationSummary;
39+
if (!Enum.TryParse(ValidationSummaryValue, ignoreCase: true, result: out validationSummary))
40+
{
41+
throw new InvalidOperationException(
42+
Resources.FormatValidationSummaryTagHelper_InvalidValidationSummaryValue(
43+
"validation-summary",
44+
ValidationSummary.All.ToString(),
45+
ValidationSummary.ModelOnly.ToString(),
46+
ValidationSummary.None.ToString()));
47+
}
48+
else if (validationSummary == ValidationSummary.None)
49+
{
50+
return;
51+
}
5852

53+
var validationModelErrorsOnly = validationSummary == ValidationSummary.ModelOnly;
5954
var tagBuilder = Generator.GenerateValidationSummary(
6055
ViewContext,
6156
validationModelErrorsOnly,

test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationSummaryTagHelperTest.cs

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
2626
var metadataProvider = new DataAnnotationsModelMetadataProvider();
2727
var validationSummaryTagHelper = new ValidationSummaryTagHelper
2828
{
29-
ValidationSummary = true
29+
ValidationSummaryValue = "All"
3030
};
3131

3232
var tagHelperContext = new TagHelperContext(new Dictionary<string, object>());
@@ -64,19 +64,18 @@ public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedPar
6464
// Arrange
6565
var validationSummaryTagHelper = new ValidationSummaryTagHelper
6666
{
67-
ValidationSummary = true,
68-
ValidationModelErrorsOnly = true
67+
ValidationSummaryValue = "ModelOnly",
6968
};
7069
var output = new TagHelperOutput(
7170
"div",
7271
attributes: new Dictionary<string, string>(),
7372
content: "Content of validation summary");
7473
var expectedViewContext = CreateViewContext();
7574
var generator = new Mock<IHtmlGenerator>();
76-
var setup = generator.Setup(mock =>
77-
mock.GenerateValidationSummary(expectedViewContext, true, null, null, null));
78-
setup.Returns(new TagBuilder("div"));
79-
setup.Verifiable();
75+
generator
76+
.Setup(mock => mock.GenerateValidationSummary(expectedViewContext, true, null, null, null))
77+
.Returns(new TagBuilder("div"))
78+
.Verifiable();
8079

8180
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
8281

@@ -95,8 +94,7 @@ public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
9594
// Arrange
9695
var validationSummaryTagHelper = new ValidationSummaryTagHelper
9796
{
98-
ValidationSummary = true,
99-
ValidationModelErrorsOnly = true
97+
ValidationSummaryValue = "ModelOnly"
10098
};
10199
var output = new TagHelperOutput(
102100
"div",
@@ -112,13 +110,13 @@ public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
112110

113111
var expectedViewContext = CreateViewContext();
114112
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
115-
var setup = generator.Setup(mock =>
116-
mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
117-
It.IsAny<bool>(),
118-
It.IsAny<string>(),
119-
It.IsAny<string>(),
120-
It.IsAny<object>()));
121-
setup.Returns(tagBuilder);
113+
var setup = generator
114+
.Setup(mock => mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
115+
It.IsAny<bool>(),
116+
It.IsAny<string>(),
117+
It.IsAny<string>(),
118+
It.IsAny<object>()))
119+
.Returns(tagBuilder);
122120

123121
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
124122

@@ -160,14 +158,18 @@ public async Task ProcessAsync_DoesNothingIfNullValidationSummary()
160158
}
161159

162160
[Theory]
163-
[InlineData(true)]
164-
[InlineData(false)]
165-
public async Task ProcessAsync_GeneratesValidationSummaryIfModelErrorsNotNull(bool validationModelErrorsOnly)
161+
[InlineData("All")]
162+
[InlineData("all")]
163+
[InlineData("ModelOnly")]
164+
[InlineData("modelonly")]
165+
[InlineData("None")]
166+
[InlineData("none")]
167+
public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone_IgnoresCase(string validationSummary)
166168
{
167169
// Arrange
168170
var validationSummaryTagHelper = new ValidationSummaryTagHelper
169171
{
170-
ValidationModelErrorsOnly = validationModelErrorsOnly
172+
ValidationSummaryValue = validationSummary
171173
};
172174
var output = new TagHelperOutput(
173175
"div",
@@ -180,14 +182,14 @@ public async Task ProcessAsync_GeneratesValidationSummaryIfModelErrorsNotNull(bo
180182

181183
var expectedViewContext = CreateViewContext();
182184
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
183-
var setup = generator.Setup(mock =>
184-
mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
185-
It.IsAny<bool>(),
186-
It.IsAny<string>(),
187-
It.IsAny<string>(),
188-
It.IsAny<object>()));
189-
setup.Returns(tagBuilder);
190-
setup.Verifiable();
185+
generator
186+
.Setup(mock => mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
187+
It.IsAny<bool>(),
188+
It.IsAny<string>(),
189+
It.IsAny<string>(),
190+
It.IsAny<object>()))
191+
.Returns(tagBuilder)
192+
.Verifiable();
191193

192194
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
193195

@@ -197,58 +199,42 @@ public async Task ProcessAsync_GeneratesValidationSummaryIfModelErrorsNotNull(bo
197199
// Assert
198200
Assert.Equal("div", output.TagName);
199201
Assert.Empty(output.Attributes);
200-
Assert.Equal("Content of validation messageNew HTML", output.Content);
201-
generator.Verify();
202+
203+
if (!validationSummary.Equals("None", StringComparison.OrdinalIgnoreCase))
204+
{
205+
Assert.Equal("Content of validation messageNew HTML", output.Content);
206+
generator.Verify();
207+
}
208+
else
209+
{
210+
Assert.Equal("Content of validation message", output.Content);
211+
}
202212
}
203213

204-
[Theory]
205-
[InlineData(true)]
206-
[InlineData(false)]
207-
public async Task ProcessAsync_GeneratesValidationSummaryWhenProvided(bool validationSummary)
214+
[Fact]
215+
public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
208216
{
209217
// Arrange
210218
var validationSummaryTagHelper = new ValidationSummaryTagHelper
211219
{
212-
ValidationSummary = validationSummary
220+
ValidationSummaryValue = "Hello World"
213221
};
214222
var output = new TagHelperOutput(
215223
"div",
216224
attributes: new Dictionary<string, string>(),
217225
content: "Content of validation message");
218-
var tagBuilder = new TagBuilder("span2")
219-
{
220-
InnerHtml = "New HTML"
221-
};
222-
223226
var expectedViewContext = CreateViewContext();
224-
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
225-
var setup = generator.Setup(mock =>
226-
mock.GenerateValidationSummary(It.IsAny<ViewContext>(),
227-
It.IsAny<bool>(),
228-
It.IsAny<string>(),
229-
It.IsAny<string>(),
230-
It.IsAny<object>()));
231-
setup.Returns(tagBuilder);
232-
setup.Verifiable();
233-
234-
SetViewContextAndGenerator(validationSummaryTagHelper, expectedViewContext, generator.Object);
227+
var expectedMessage = "Cannot determine validation-summary. Acceptable values are 'All', 'ModelOnly' " +
228+
"and 'None'.";
235229

236230
// Act
237-
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
231+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
232+
{
233+
await validationSummaryTagHelper.ProcessAsync(context: null, output: output);
234+
});
238235

239236
// Assert
240-
Assert.Equal("div", output.TagName);
241-
Assert.Empty(output.Attributes);
242-
243-
if (validationSummary)
244-
{
245-
Assert.Equal("Content of validation messageNew HTML", output.Content);
246-
generator.Verify();
247-
}
248-
else
249-
{
250-
Assert.Equal("Content of validation message", output.Content);
251-
}
237+
Assert.Equal(expectedMessage, ex.Message);
252238
}
253239

254240
private static ViewContext CreateViewContext()

0 commit comments

Comments
 (0)