Skip to content

Commit

Permalink
Suppress validation summary <div> when nothing is generated
Browse files Browse the repository at this point in the history
- aspnet#2372
- make the `ValidationSummaryTagHelper` behave consistently with `Html.ValidationSummar()`
  • Loading branch information
dougbu committed Sep 27, 2016
1 parent 6cdd045 commit 0a4c06e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
message: null,
headerTag: null,
htmlAttributes: null);
if (tagBuilder != null)
if (tagBuilder == null)
{
output.MergeAttributes(tagBuilder);
output.PostContent.AppendHtml(tagBuilder.InnerHtml);
// The generator determined no element was necessary.
output.SuppressOutput();
return;
}

output.MergeAttributes(tagBuilder);
output.PostContent.AppendHtml(tagBuilder.InnerHtml);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<div class="order validation-summary-errors" data-valmsg-summary="true"><ul><li>The value &#x27;&#x27; is invalid.</li>
<li>The Password field is required.</li>
</ul></div>
<div class="order"></div>

<input type="submit"/>
<input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</div>
<div class="order validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
<div class="order"></div>

<input type="submit"/>
<input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TestCommon;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -96,7 +97,7 @@ public async Task ProcessAsync_GeneratesExpectedOutput_WithNoErrors(ModelStateDi

[Theory]
[MemberData(nameof(ProcessAsync_GeneratesExpectedOutput_WithNoErrorsData))]
public async Task ProcessAsync_DoesNothingIfClientSideValiationDisabled_WithNoErrorsData(
public async Task ProcessAsync_SuppressesOutput_IfClientSideValiationDisabled_WithNoErrorsData(
ModelStateDictionary modelStateDictionary)
{
// Arrange
Expand Down Expand Up @@ -128,16 +129,12 @@ public async Task ProcessAsync_DoesNothingIfClientSideValiationDisabled_WithNoEr
await validationSummaryTagHelper.ProcessAsync(context, output);

// Assert
Assert.Equal("div", output.TagName);
Assert.Null(output.TagName);
Assert.Empty(output.Attributes);
Assert.False(output.IsContentModified);
Assert.False(output.PreContent.IsModified);
Assert.False(output.PreElement.IsModified);
Assert.False(output.PostContent.IsModified);
Assert.False(output.PostElement.IsModified);
Assert.Empty(HtmlContentUtilities.HtmlContentToString(output));
}

public static TheoryData<string, ModelStateDictionary> ProcessAsync_DoesNothingIfModelOnly_WithNoModelErrorData
public static TheoryData<string, ModelStateDictionary> ProcessAsync_SuppressesOutput_IfModelOnlyWithNoModelErrorData
{
get
{
Expand Down Expand Up @@ -165,8 +162,8 @@ public static TheoryData<string, ModelStateDictionary> ProcessAsync_DoesNothingI
}

[Theory]
[MemberData(nameof(ProcessAsync_DoesNothingIfModelOnly_WithNoModelErrorData))]
public async Task ProcessAsync_DoesNothingIfModelOnly_WithNoModelError(
[MemberData(nameof(ProcessAsync_SuppressesOutput_IfModelOnlyWithNoModelErrorData))]
public async Task ProcessAsync_SuppressesOutput_IfModelOnly_WithNoModelError(
string prefix,
ModelStateDictionary modelStateDictionary)
{
Expand Down Expand Up @@ -199,13 +196,9 @@ public async Task ProcessAsync_DoesNothingIfModelOnly_WithNoModelError(
await validationSummaryTagHelper.ProcessAsync(context, output);

// Assert
Assert.Equal("div", output.TagName);
Assert.Null(output.TagName);
Assert.Empty(output.Attributes);
Assert.False(output.IsContentModified);
Assert.False(output.PreContent.IsModified);
Assert.False(output.PreElement.IsModified);
Assert.False(output.PostContent.IsModified);
Assert.False(output.PostElement.IsModified);
Assert.Empty(HtmlContentUtilities.HtmlContentToString(output));
}

[Theory]
Expand Down

0 comments on commit 0a4c06e

Please sign in to comment.