Skip to content

Add CssClass property to ValidationSummary#43928

Closed
ChristopherHaws wants to merge 2 commits into
dotnet:mainfrom
ChristopherHaws:chaws/validation-error-css-class
Closed

Add CssClass property to ValidationSummary#43928
ChristopherHaws wants to merge 2 commits into
dotnet:mainfrom
ChristopherHaws:chaws/validation-error-css-class

Conversation

@ChristopherHaws
Copy link
Copy Markdown
Contributor

@ChristopherHaws ChristopherHaws commented Sep 12, 2022

Add CssClass property to ValidationSummary

API Proposal: #46461

Description

Add CssClass property to ValidationSummary and append the contents to the resulting class attribute.

Fixes #43860, Closes: #46461

@ChristopherHaws ChristopherHaws requested review from a team as code owners September 12, 2022 20:06
@ghost ghost added area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member labels Sep 12, 2022
@ghost
Copy link
Copy Markdown

ghost commented Sep 12, 2022

Thanks for your PR, @ChristopherHaws. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Comment thread src/Components/Web/test/Forms/ValidationSummaryTest.cs Outdated
Comment thread src/Components/Web/src/Forms/ValidationSummary.cs Outdated
@ChristopherHaws ChristopherHaws force-pushed the chaws/validation-error-css-class branch from bd6eeba to 765808d Compare September 13, 2022 19:45
@ChristopherHaws
Copy link
Copy Markdown
Contributor Author

@SteveSandersonMS I updated the test to be an E2E test. I had a hell of a time getting selenium to work because I have v105 of chrome installed but the web driver used by the E2E tests is still v103 and chrome doesn't supply old versions of chrome. I had to lookup the build number of chromium that corresponds to v103 of chrome and use that instead. I remember now why I stopped using selenium and switched to playwright. :)

I think this PR is ready for review now. Thanks!

@mkArtakMSFT
Copy link
Copy Markdown
Contributor

Thanks for your PR, @ChristopherHaws.
@SteveSandersonMS could you please review this? Thanks!

@ghost
Copy link
Copy Markdown

ghost commented Jan 25, 2023

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no breaking changes are introduced, please remove the pending ci rerun label to kick off a new CI run.

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 25, 2023
@mkArtakMSFT
Copy link
Copy Markdown
Contributor

@Nick-Stanton reassigning this to you as @SteveSandersonMS has been busy with other things.

Copy link
Copy Markdown
Member

@Nick-Stanton Nick-Stanton left a comment

Choose a reason for hiding this comment

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

Hi @ChristopherHaws, thank you for your contribution and apologies for the delay. I made one small request, but otherwise the changes look good.

Since your PR contains new API, you will need to create an API Proposal to go along with your issue. This is done by opening a new issue using the API Proposal template. More information about the process is here. Once the proposal is ready, I will "champion" your changes on your behalf during review. Please let me know if you have any questions.

Comment thread src/Components/Web/src/PublicAPI.Shipped.txt Outdated
@Nick-Stanton Nick-Stanton removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 27, 2023
@ghost
Copy link
Copy Markdown

ghost commented Jan 27, 2023

/azp run

@azure-pipelines
Copy link
Copy Markdown

Commenter does not have sufficient privileges for PR 43928 in repo dotnet/aspnetcore

@Nick-Stanton
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@ghost
Copy link
Copy Markdown

ghost commented Feb 5, 2023

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no breaking changes are introduced, please remove the pending ci rerun label to kick off a new CI run.

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 5, 2023
@ChristopherHaws ChristopherHaws force-pushed the chaws/validation-error-css-class branch from 2894645 to aabd544 Compare February 5, 2023 05:11
@Nick-Stanton Nick-Stanton removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2023
@ghost
Copy link
Copy Markdown

ghost commented Feb 6, 2023

/azp run

@azure-pipelines
Copy link
Copy Markdown

Commenter does not have sufficient privileges for PR 43928 in repo dotnet/aspnetcore

@Nick-Stanton
Copy link
Copy Markdown
Member

@ChristopherHaws thanks for taking the time to draft your proposal. It looks like the commit that moved your new API caused some new build failures. I imagine CI will be clean once those are fixed.

While investigating these changes deeper to prepare for API review, I had a couple questions:

  1. First, I was looking at your example in Pass css class in <ValidationSummary /> to the generated <ul> #43860. If you were to pass class="validation-errors pt-2", wouldn't you get your desired output? I ask because if that scenario works and this feature hasn't been asked for elsewhere, it may be hard to argue for this change. Example:
<ValidationSummary class="validation-errors pt-2" data-test="validation-errors" />
  1. This implementation has ValidationSummary modifying its own CssClass parameter, which you were right to ask about here Pass css class in <ValidationSummary /> to the generated <ul> #43860 (comment). We usually guide against doing this. Is there a way to achieve this functionality without directly changing CssClass?

@ChristopherHaws
Copy link
Copy Markdown
Contributor Author

ChristopherHaws commented Feb 7, 2023

@Nick-Stanton Hmm, not sure why the build would be failing. Unfortunately I don't have time to look at that this week, I am super busy at work and have a theater show I am running at night.

  1. The existing <ValidationSummary /> component adds validation-errors, not the end user.
  2. If I remember correctly (it's been almost 6 months since I wrote this code), the CssClass property needs to contain the class names being appended to the component which includes the built-in validation-errors css class.

The only built-in component I found which allows users to set CssClass is NavLink) which also appends it's built-in css class to the property.

Most of the built-in components don't allow you to pass CssClass (it is get only).

I don't know the exact reason that the built-in css class names need to be in this property but I am guessing it is one or many of the following:

  • Performance. If you append the string only one time when the parameter is set, you avoid string allocations every time the component gets rendered/re-rendered
  • If someone has a ref to the component, they could then get the CssClass and it would actually contain all of the css classes, not just the ones the user added
  • When writing tests, this would allow you to assert that the css classes are correct without needing to validate it using the DOM

@SteveSandersonMS
Copy link
Copy Markdown
Member

The only built-in component I found which allows users to set CssClass is NavLink) which also appends it's built-in css class to the property.

Ah, checking this more closely, I see that the CssClass property you're adding is not actually a [Parameter] property so is fine to mutate. (Apologies to @Nick-Stanton - I gave an incorrect suggestion about this.). However I'm not sure it should be settable by subclasses, since their changes would be overwritten every time OnParametersSet runs which would be very confusing.

The existing component adds validation-errors, not the end user.

That's right, validation-errors is the default CSS class attribute value. However it's intentional that developers can overwrite this with any other that they want. For example, maybe their styling system doesn't use that CSS class name, so they instead want to use <ValidationErrors class="my-errors">. It's intended as a feature that you can replace the class name. It's the same with ValidationMessage too.

The change in the PR, if I'm reading correctly, would prevent that and force people to have validation-errors whether or not they want it. I know in most cases that won't cause them any problem, but I'm not really sold on the idea that a lot of people are going to be using class to represent additional class names as opposed to thinking they are setting the entire class attribute. Especially given how this would then be inconsistent with ValidationMessage.

The reason why we combine classes dynamically in NavLink is that NavLink adds and removes the active class based on navigation state. There has to be some dynamic adding/removing of this class since that's the whole point of NavLink. However we still don't force any specific hardcoded class name since you can set ActiveClass to an arbitrary value.

In summary it looks as if we already have the desired functionality, i.e., the ability to customize the CSS class name and add arbitrary extra ones just by specifying class. I don't think we'd want to force people to have validation-errors. Hope that seems reasonable. If I'm missing something important, please let me know!

@ghost
Copy link
Copy Markdown

ghost commented Feb 16, 2023

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no breaking changes are introduced, please remove the pending ci rerun label to kick off a new CI run.

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 16, 2023
@ChristopherHaws
Copy link
Copy Markdown
Contributor Author

@SteveSandersonMS The current behavior of ValidationSummary is that you simply cannot override the class attribute at all. If you pass a class, it is simply ignored. Here are two examples I just tested:

<!-- <ValidationSummary /> -->
<ul class="validation-errors">
    <li class="validation-message">...</li>
</ul>

<!-- <ValidationSummary class="test" data-test="test" /> -->
<ul data-test="test" class="validation-errors">
    <li class="validation-message">...</li>
</ul>

I see your point about it being confusing as to whether it replaces or appends the class though. I am coming to blazor after using React for many years where it is pretty common for the classes you pass in to be appended, so I would have assumed that it would be appended. Here are the docs for FluentUI React where you can see that className is an Optional class name that is added to the container of the component.

I get that this is not React, so I looked at one of the top Blazor Component libraries to see what they do. MudBlazor defines Class as User class names, separated by space. - The key word here being that this is the "User"'s class names.

@SteveSandersonMS
Copy link
Copy Markdown
Member

@ChristopherHaws Can you provide a repro using an online REPL?

I just tried it and it works for me: https://blazorrepl.telerik.com/QRumvqaN29a37x4K49

image

@ChristopherHaws
Copy link
Copy Markdown
Contributor Author

Interesting, I had used BlazorFiddle to try it. I guess they are on an old version of Blazor. It's been a while since I was dealing with this issue in our project (we ended up just wrapping ValidationSummary in our own component).
image

@Nick-Stanton
Copy link
Copy Markdown
Member

@ChristopherHaws I just looked into it and it looks like they're using a really old version of Blazor.
image
Apologies for not discovering the functionality earlier in review. Do you still have interest in pursuing this PR? It seems like the ask is already met.

@mkArtakMSFT
Copy link
Copy Markdown
Contributor

Closing as this is already supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

4 participants