Merge pull request #8123 from kishanAnem/dev

Custom error messages with validation message tag helper #8035
This commit is contained in:
Doug Bunting 2018-07-22 11:05:53 -07:00 committed by GitHub
commit ff4e2eb4c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View File

@ -71,11 +71,24 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
}; };
} }
string message = null;
if (!output.IsContentModified)
{
var tagHelperContent = await output.GetChildContentAsync();
// We check for whitespace to detect scenarios such as:
// <span validation-for="Name">
// </span>
if (!tagHelperContent.IsEmptyOrWhiteSpace)
{
message = tagHelperContent.GetContent();
}
}
var tagBuilder = Generator.GenerateValidationMessage( var tagBuilder = Generator.GenerateValidationMessage(
ViewContext, ViewContext,
For.ModelExplorer, For.ModelExplorer,
For.Name, For.Name,
message: null, message: message,
tag: null, tag: null,
htmlAttributes: htmlAttributes); htmlAttributes: htmlAttributes);
@ -84,26 +97,11 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
output.MergeAttributes(tagBuilder); output.MergeAttributes(tagBuilder);
// Do not update the content if another tag helper targeting this element has already done so. // Do not update the content if another tag helper targeting this element has already done so.
if (!output.IsContentModified) if (!output.IsContentModified && tagBuilder.HasInnerHtml)
{
// We check for whitespace to detect scenarios such as:
// <span validation-for="Name">
// </span>
var childContent = await output.GetChildContentAsync();
if (childContent.IsEmptyOrWhiteSpace)
{
// Provide default message text (if any) since there was nothing useful in the Razor source.
if (tagBuilder.HasInnerHtml)
{ {
output.Content.SetHtmlContent(tagBuilder.InnerHtml); output.Content.SetHtmlContent(tagBuilder.InnerHtml);
} }
} }
else
{
output.Content.SetHtmlContent(childContent);
}
}
}
} }
} }
} }

View File

@ -357,10 +357,11 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
} }
[Theory] [Theory]
[InlineData("Content of validation message", "Content of validation message")] [InlineData("Content of validation message", "Content of validation message", "New HTML")]
[InlineData("\r\n \r\n", "New HTML")] [InlineData("\r\n \r\n", null, "New HTML")]
public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage( public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
string childContent, string childContent,
string expectedMessage,
string expectedOutputContent) string expectedOutputContent)
{ {
// Arrange // Arrange
@ -375,7 +376,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
It.IsAny<ViewContext>(), It.IsAny<ViewContext>(),
It.IsAny<ModelExplorer>(), It.IsAny<ModelExplorer>(),
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), expectedMessage,
It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<object>())) It.IsAny<object>()))
.Returns(tagBuilder); .Returns(tagBuilder);