This repository was 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
Add ValidationSummaryTagHelper. #1340
Closed
NTaylorMullen
wants to merge
1
commit into
TagHelpers_ValidationMessage
from
TagHelpers_ValidationSummary
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Microsoft.AspNet.Mvc.TagHelpers/Properties/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.TagHelpers.Test")] |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNet.Mvc.TagHelpers/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNet.Mvc.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Acceptable validation summary rendering modes. | ||
/// </summary> | ||
public enum ValidationSummary | ||
{ | ||
/// <summary> | ||
/// No validation summary. | ||
/// </summary> | ||
None, | ||
|
||
/// <summary> | ||
/// Validation summary with model-level errors only (excludes all property errors). | ||
/// </summary> | ||
ModelOnly, | ||
|
||
/// <summary> | ||
/// Validation summary with all errors. | ||
/// </summary> | ||
All | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Microsoft.AspNet.Mvc.TagHelpers/ValidationSummaryTagHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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.AspNet.Mvc.Rendering; | ||
using Microsoft.AspNet.Razor.Runtime.TagHelpers; | ||
using Microsoft.AspNet.Razor.TagHelpers; | ||
|
||
namespace Microsoft.AspNet.Mvc.TagHelpers | ||
{ | ||
/// <summary> | ||
/// <see cref="ITagHelper"/> implementation targeting <div> elements with a <c>validation-summary</c> | ||
/// attribute. | ||
/// </summary> | ||
[TagName("div")] | ||
[ContentBehavior(ContentBehavior.Append)] | ||
public class ValidationSummaryTagHelper : TagHelper | ||
{ | ||
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing. | ||
[Activate] | ||
protected internal ViewContext ViewContext { get; set; } | ||
|
||
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing. | ||
[Activate] | ||
protected internal IHtmlGenerator Generator { get; set; } | ||
|
||
// TODO: Change to ValidationSummary enum once https://github.com/aspnet/Razor/issues/196 has been completed. | ||
/// <summary> | ||
/// If <c>All</c> or <c>ModelOnly</c>, appends a validation summary. Acceptable values are defined by the | ||
/// <see cref="ValidationSummary"/> enum. | ||
/// </summary> | ||
[HtmlAttributeName("validation-summary")] | ||
public string ValidationSummaryValue { get; set; } | ||
|
||
/// <inheritdoc /> | ||
/// Does nothing if <see cref="ValidationSummaryValue"/> is <c>null</c>, empty or "None". | ||
public override void Process(TagHelperContext context, TagHelperOutput output) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add usual |
||
{ | ||
if (!string.IsNullOrEmpty(ValidationSummaryValue)) | ||
{ | ||
ValidationSummary validationSummaryValue; | ||
if (!Enum.TryParse(ValidationSummaryValue, ignoreCase: true, result: out validationSummaryValue)) | ||
{ | ||
throw new InvalidOperationException( | ||
Resources.FormatValidationSummaryTagHelper_InvalidValidationSummaryValue( | ||
"<div>", | ||
"validation-summary", | ||
ValidationSummaryValue, | ||
ValidationSummary.All, | ||
ValidationSummary.ModelOnly, | ||
ValidationSummary.None)); | ||
} | ||
else if (validationSummaryValue == ValidationSummary.None) | ||
{ | ||
return; | ||
} | ||
|
||
var validationModelErrorsOnly = validationSummaryValue == ValidationSummary.ModelOnly; | ||
var tagBuilder = Generator.GenerateValidationSummary( | ||
ViewContext, | ||
excludePropertyErrors: validationModelErrorsOnly, | ||
message: null, | ||
headerTag: null, | ||
htmlAttributes: null); | ||
|
||
if (tagBuilder != null) | ||
{ | ||
output.MergeAttributes(tagBuilder); | ||
output.Content += tagBuilder.InnerHtml; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just
ValidationSummary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conflicts with the enum and ends up with ugliness in the Process method wherever we reference the enum.
Ex if we were to have the property be ValidationSummary:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conflicts how? we do this elsewhere and everything Just Works(tm) e.g.
in
IHtmlHelper
. (yes, theHtml5DateRenderingMode
type is anenum
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The types are different, otherwise it'd work. If you refactor
ValidationSummaryValue
=>ValidationSummary
VS does what I put in the snippet because it can't compile otherwise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange that what seems the more complicated case just works. pity.