-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mechanism for providing validation CSS class names through DI #34777
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiercn didn't you look at this at one point?
public DefaultHtmlGenerator( | ||
IAntiforgery antiforgery, | ||
IOptions<MvcViewOptions> optionsAccessor, | ||
IModelMetadataProvider metadataProvider, | ||
IUrlHelperFactory urlHelperFactory, | ||
HtmlEncoder htmlEncoder, | ||
ValidationHtmlAttributeProvider validationAttributeProvider) | ||
ValidationHtmlAttributeProvider validationAttributeProvider, | ||
IValidationCssClassNameProvider validationCssClassNameProvider) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make these properties on HtmlHelperOptions
rather than a separate type? We generally don't expect users to customize options by injecting new DI services. That also avoids a bunch of source / binary breaking changes that were introduced as part of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, I had not spotted that type. That sounds like a good alternative that has less impact on the code base.
What binary breaking change do you refer to? Because the HtmlHelper properties will still be removed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would keep the HtmlHelper properties as is since removing them would be breaking. Adding a new constructor parameter is also a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is a much better approach. I implemented it. I did not yet re-add the static fields to HtmlHelper, as it might be confusing for people that these variables are defined there while they are not used anymore... Should we annotate them Obsolete, or add some comment?
Adapt tests to incorporate new options
@FWest98 do you mind updating the issue with an API proposal? Here's the template for it: Proposed APIPlease provide the specific public API signature diff that you are proposing. For example: namespace Microsoft.AspNetCore.Http
{
public static class HttpResponseWritingExtensions
{
+ public Task WriteAsync(this HttpResponse response, StringBuilder builder);
} You may find the Framework Design Guidelines helpful. Usage ExamplesPlease provide code examples that highlight how the proposed API additions are meant to be consumed. // some lines of code here Alternative DesignsWere there other options you considered, such as alternative API shapes? RisksPlease mention any risks that to your knowledge the API proposal might entail, such as breaking changes, performance regressions, etc. |
The API proposal can be found in #17412 (comment) |
I don't think this change is enough. I believe this will not work with client-side validation. The other aspect to consider here is that if we open the way to configure these things we should do it in a way is configurable locally to a given view rather than having a global default that can interfere with existing libraries. |
As I mentioned, this indeed will not change the client-side validation. However, there is a relatively easy way to change that yourself (ref: https://gist.github.com/FWest98/9141b3c2f260bee0e46058897d2017d2#file-_validationscriptspartial-cshtml as an example). Furthermore, not everyone uses the default client-side validation, but they might roll their own solution. Those people will still benefit from this change. I think this narrows down to properly documenting the side-effects of changing the default and documenting the manual change to fix the client-side validation. I agree that it would be much better to have this configurable in a view, but that would require a lot more engineering work (and would be even more complicated to integrate in the client-side validation). I think this would be a great first step, considering that this feature has been requested for almost 2 years now. Of course, I am open to contribute a more extensive solution for this; but I am not sure what the best design would be for either the client-side validation or the per-view configuration. |
This is not a behavior we want to have. If you change the validation you expect it to work in both places, that's been the case for this feature since the beginning of MVC, that's a breaking change we aren't willing to make I think. |
How would you propose to integrate updating the client-side validation as well? I would see a few options:
|
@javiercn Could you maybe give some directions on how to implement the client-side validation properly as well? I posted some suggestions before, and I would be happy to help implement a full solution! |
Adding mechanism for providing validation CSS class names through DI
This PR adds an interface for providing CSS class names for field/form validation with an associated default implementation. Previously, these were hardcoded in read-only static variables. Now, users can provide their own implementation and thus their own class names to fit any front-end framework, by simply providing a custom implementation of the interface.
PR Description
This is a first shot at fixing #17412. I hope this is the desired format/solution approach. I am open to suggestions on other approaches. There are no added tests, since testing for the right class names is already included in many other tests. I am able to add dedicated tests for delivering a custom implementation, but that seemed a bit overkill to me, since none of these strings are hardcoded anywhere else in the codebase.
I believe this technically does change the API, since a number of previously-public fields are now gone. Personally, I would think that removing these in the next major release would be acceptable, but you might beg to differ. In that case, I would see a number of approaches:
HtmlHelper
as they always were, but that might be a bit confusingThis PR would probably need some follow-up:
Fixes #17412