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

Custom error messages with validation message tag helper #8035 #8087

Closed
wants to merge 0 commits into from

Conversation

kishanAnem
Copy link
Contributor

Hi @dougbu

I think I fixed this bug. Is this right way to fix?
If yes, I'll add test cases.

Copy link
Member

@dougbu dougbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very close. And, yes, needs tests.

Separately, please rebase your branch on our release/2.2. Although not messing up the comparison, 6 extra commits are visible.

@@ -71,11 +71,13 @@ public ValidationMessageTagHelper(IHtmlGenerator generator)
};
}

var childContent = await output.GetChildContentAsync();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set childContent to null if output.IsContentModified; the GetChildContentAsync() call is expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure.

var tagBuilder = Generator.GenerateValidationMessage(
ViewContext,
For.ModelExplorer,
For.Name,
message: null,
message: childContent.IsEmptyOrWhiteSpace ? null : childContent.GetContent(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition also needs to check output.IsContentModified and to pass message: null in that case.

@dougbu
Copy link
Member

dougbu commented Jul 15, 2018

By the way, test failures are caused by not checking output.IsContentModified where I mentioned.

@@ -89,7 +91,7 @@ public ValidationMessageTagHelper(IHtmlGenerator generator)
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have mentioned:

  • This comment is no longer correct since message may have come from the Razor source now
  • The else block starting at line 103 is no longer needed
  • The comment above about whitespace belongs above the GenerateValidationMessage(...) call

I suspect this block could now be narrowed to:

if (!output.IsContentModified && tagBuilder.HasInnerHtml))
{
    output.Content.SetHtmlContent(tagBuilder.InnerHtml);
}

@kishanAnem
Copy link
Contributor Author

Hi @dougbu

But I'm able to run tests from CMD line. I'm getting following error, when I run tests on visual studio.
Sorry for asking this favor. iIf you have time, please help me out.

[17-07-2018 09:01:26 PM Informational] ------ Run test started ------
[17-07-2018 09:02:59 PM Error] Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process exited with error: It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '2.2.0-preview1-26618-02' was not found.

  • Check application dependencies and target a framework version installed at:
    C:\Program Files\dotnet\
  • Installing .NET Core prerequisites might help resolve this problem:
    http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
  • The .NET Core framework and SDK can be installed from:
    https://aka.ms/dotnet-download
  • The following versions are installed:
    1.0.5 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.0.9 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.0.10 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.0.11 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.1.0-preview1-001100-00 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.1.2 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.1.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.1.7 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    1.1.8 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.0.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.0.5 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.0.7 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.1.0-preview1-26216-03 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.1.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.2.0-preview1-26527-01 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.2.0-preview1-26609-01 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    2.2.0-preview1-26609-02 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    3.0.0-preview1-26621-01 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

@dougbu
Copy link
Member

dougbu commented Jul 17, 2018

@kishanAnem this likely means your Path environment variable includes C:\Program Files\dotnet. Remove that and add %UserProfile%\.dotnet\x64. Then restart Visual Studio.

@kishanAnem
Copy link
Contributor Author

Thank you very much. It worked... 👍

@kishanAnem
Copy link
Contributor Author

kishanAnem commented Jul 19, 2018

Hi @dougbu

commit title is misleading sorry for that tried to revert.
Please let me know fix is correct. 2 functional test are failed. if fix is correct, will fix function test cases.

Copy link
Member

@dougbu dougbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better but lost the IsEmptyOrWhiteSpace check

@@ -71,37 +71,29 @@ public ValidationMessageTagHelper(IHtmlGenerator generator)
};
}

string message = null;
TagHelperContent tagHelperContent = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this outside the next block i.e. just use var tagHelperContent = await output.GetChildContentAsync();

if (!output.IsContentModified)
{
tagHelperContent = await output.GetChildContentAsync();
message = tagHelperContent.GetContent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check tagHelperContent.IsEmptyOrWhiteSpace before updating message.

var tagBuilder = Generator.GenerateValidationMessage(
ViewContext,
For.ModelExplorer,
For.Name,
message: null,
message: output.IsContentModified ? null : message,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just message since it's null if output.IsContentModified

@@ -358,16 +358,16 @@ public class ValidationMessageTagHelperTest

[Theory]
[InlineData("Content of validation message", "Content of validation message")]
[InlineData("\r\n \r\n", "New HTML")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should work as it did before.


var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var setup = generator
.Setup(mock => mock.GenerateValidationMessage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setup should confirm the expected message is passed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

validationMessageTagHelper.ViewContext = viewContext;

// Act
validationMessageTagHelper.ProcessAsync(context, output).Wait();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're calling ProcessAsync(...), use await and change the method's signature to include async Task

Copy link
Member

@dougbu dougbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost…

tag: null,
htmlAttributes: htmlAttributes);

if (tagBuilder != null)
{
output.MergeAttributes(tagBuilder);

// Do not update the content if another tag helper targeting this element has already done so.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore this comment

if (!output.IsContentModified)
{
// We check for whitespace to detect scenarios such as:
// <span validation-for="Name">
// </span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore this comment but above line 78

// </span>
var childContent = await output.GetChildContentAsync();
if (childContent.IsEmptyOrWhiteSpace)
if (string.IsNullOrWhiteSpace(message) && tagBuilder.HasInnerHtml)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of the string.IsNullOrWhiteSpace(message) check and the else because whitespace should be impossible given the earlier tagHelperContent.IsEmptyOrWhiteSpace check and the IHtmlGenerator has already done the right thing with message. This block should just be

if (!output.IsContentModified && tagBuilder.HasInnerHtml)
{
    output.Content.SetHtmlContent(tagBuilder.InnerHtml);
}

@@ -364,10 +364,11 @@ public class ValidationMessageTagHelperTest
string expectedOutputContent)
{
// Arrange
var tagBuilder = new TagBuilder("span2");
var tagBuilder = new TagBuilder("span");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking more closely at these tests, I see ProcessAsync_MergesTagBuilderFromGenerateWithoutValidationMessage() should be redundant. Remove the new test and revert current changes to ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(...). Then, add an expectedMessage parameter and the right data values ("Content of validation message" and null) and use expectedMessage in the Setup(...) at line 379.

Don't add the ProcessAsync_MergesTagBuilderFromGenerateWithoutValidationMessage(...) case ([InlineData(null, null, "New HTML")]) here because there's a bug in DefaultTagHelperContent.IsEmptyOrWhiteSpaceCore(). It returns false when the only content is null.

Congrats on unearthing that aspnet/Razor bug!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed aspnet/Razor#2497 and am doing a quick fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this happens with great support.

It.IsAny<ViewContext>(),
It.IsAny<ModelExplorer>(),
It.IsAny<string>(),
"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we always use string.Empty unless a constant is required


var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var setup = generator
.Setup(mock => mock.GenerateValidationMessage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@dougbu
Copy link
Member

dougbu commented Jul 22, 2018

@kishanAnem please git rebase -i HEAD~9 and delete the first 6 commits. Those commits don't contribute real differences, just confuse the branches.

(HEAD~9 might need to be HEAD~10 or more if you've added new commits locally.)

@dougbu
Copy link
Member

dougbu commented Jul 22, 2018

And, ignore the VSTS failure. Not sure why the CacheTagHelperTest.ProcessAsync_AwaitersUseTheResultOfExecutor(...) test isn't working there. But, it's definitely not related to changes in this PR.

@kishanAnem
Copy link
Contributor Author

Hi @dougbu ,
Sorry.
I'm trying to git rebase -i HEAD~9 . I endup to two more commits.
I think you understand i'm naive to GIT. I will revert those commits.

@kishanAnem
Copy link
Contributor Author

Hi @dougbu
I think revert these changes also. Will open new PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants