Custom error messages with validation message tag helper #8035
#8035 PR #8087 https://github.com/aspnet/Razor/issues/2497
This commit is contained in:
parent
e1e7ec0f28
commit
c0ba374549
|
|
@ -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(
|
||||
ViewContext,
|
||||
For.ModelExplorer,
|
||||
For.Name,
|
||||
message: null,
|
||||
message: message,
|
||||
tag: null,
|
||||
htmlAttributes: htmlAttributes);
|
||||
|
||||
|
|
@ -84,27 +97,12 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
output.MergeAttributes(tagBuilder);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Content.SetHtmlContent(childContent);
|
||||
}
|
||||
output.Content.SetHtmlContent(tagBuilder.InnerHtml);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -357,10 +357,11 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Content of validation message", "Content of validation message")]
|
||||
[InlineData("\r\n \r\n", "New HTML")]
|
||||
[InlineData("Content of validation message", "Content of validation message", "New HTML")]
|
||||
[InlineData("\r\n \r\n", null, "New HTML")]
|
||||
public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
|
||||
string childContent,
|
||||
string expectedMessage,
|
||||
string expectedOutputContent)
|
||||
{
|
||||
// Arrange
|
||||
|
|
@ -375,7 +376,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
It.IsAny<ViewContext>(),
|
||||
It.IsAny<ModelExplorer>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
expectedMessage,
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<object>()))
|
||||
.Returns(tagBuilder);
|
||||
|
|
|
|||
Loading…
Reference in New Issue