-
Notifications
You must be signed in to change notification settings - Fork 2.1k
replacement for html.GetUnobtrusiveValidationAttributes html.GetClientValidationRules #5028
Comments
@dougbu ? |
@rynowak I think you worked in this area too? |
In RC1, For this use case, I suggest extending public IHtmlContent MyTextBox(
this IHtmlHelper helper,
string expression,
object value,
string format,
object htmlAttributes)
{
var content = helper.TextBox(expression, value, format, htmlAttributes);
var tagBuilder = content as TagBuilder;
if (tagBuilder != null)
{
tagBuilder.AddCssClass("classy");
}
return content;
} |
so is there a way now to get the validation attributes ?
|
If looking for the attributes a specific helper would add, could do something like I showed above but with var dictionary = tagBuilder.Attributes
.Where(item => item.Key.StartsWith("data-", StringComparison.OrdinalIgnoreCase))
.ToDictionary(keySelector: item => item.Key, elementSelector: item => item.Value); Could also subclass public class MyHtmlGenerator : DefaultHtmlGenerator
{
public MyHtmlGenerator(
IAntiforgery antiforgery,
IOptions<MvcViewOptions> optionsAccessor,
IModelMetadataProvider metadataProvider,
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder,
ClientValidatorCache clientValidatorCache)
: base(
antiforgery,
optionsAccessor,
metadataProvider,
urlHelperFactory,
htmlEncoder,
clientValidatorCache)
{
}
public new void AddValidationAttributes(...)
{
// ...
}
} Then place var generator = helper.ViewContext.HtmlContext.RequestServices.GetRequiredService<IHtmlGenerator>() as MyHtmlGenerator Unfortunately |
ok, so the way to get the validation attributes now is to generate an editor (textbox, hidden input), cast it as TagBuilder and after filter the TagBuilder.Attributes, will this get easier in the future ? |
I don't believe that inspecting the TagBuilder attributes produced by a random other helper is a recommended approach. @dougbu can we just add an interface / API that exposes the required information? I don't think any of the examples given here are supported or recommended... |
@Eilon sure, we can add an @omuleanu exactly what information do you need? In particular would a new method similar to the existing |
I need to get the collection of js validation attributes, this method was present in mvc5, 4, 3
|
@dougbu is this going to be implemented any time soon ? |
@omuleanu I don't know. @danroth27, @Eilon this doesn't have a milestone and seems to be missing all labels. Suggest this would be an Enhancement. |
…alidationAttributes()` available - #5028 - helpers similar to our HTML or tag helpers can use the new singleton to examine validation attributes - in the most common case, helpers add validation attributes to a `TagBuilder` but that is not required - separating the `ValidationAttributesProvider` from `DefaultHtmlGenerator` avoids creating two instances of that singleton - would be even uglier to require callers to cast an `IHtmlGenerator` to `IValidationAttributeProvider`
Slight side note @Eilon:
Early on I mentioned subclassing More generally, we force creating a @ajaybhargavb tells me Should we move |
So since this is a milestone for 1.1.0, is there a workaround to get access to these validation attributes now? So far all I can think of is to manually loop through the attributes and check if each validation attribute exist on the property, and handle each individually, which is less than ideal. |
OP in #5240 provided his workaround for pre-1.1.0. |
…alidationAttributes()` available - #5028 - helpers similar to our HTML or tag helpers can use the new singleton to examine validation attributes - in the most common case, helpers add validation attributes to a `TagBuilder` but that is not required - separating the `ValidationAttributesProvider` from `DefaultHtmlGenerator` avoids creating two instances of that singleton - would be even uglier to require callers to cast an `IHtmlGenerator` to `IValidationAttributeProvider`
…s()` available - #5028 - helpers similar to our HTML or tag helpers can use the new singleton to examine or add validation attributes - in the most common case, helpers add validation attributes to a `TagBuilder` - separate `DefaultValidationHtmlAttributeProvider` from `DefaultHtmlGenerator` - avoids creating two instances of the `DefaultHtmlGenerator` singleton - would be even uglier to require callers to cast an `IHtmlGenerator` to `ValidationHtmlAttributeProvider` - `[Obsolete]` old `DefaultHtmlGenerator` constructor
hi @dougbu |
Get the public IHtmlContent MyTextBox(
string expression,
object value,
string format,
object htmlAttributes)
{
var tagBuilder = new TagBuilder(...);
tagBuilder.AddCssClass("classy");
// Get ModelExplorer and do other custom helper stuff...
// Add the validation attributes.
_validationAttributeProvider.AddAndTrackValidationAttributes(
viewContext,
modelExplorer,
expression,
tagBuilder.Attributes);
return tagBuilder;
} |
@dougbu not sure I understand you correctly,
can this ^ method be improved now, or it remains the same ? |
If you just want the dictionary, the following would work: var attributes = new Dictionary<string, string>();
_validationAttributeProvider.AddAndTrackValidationAttributes(
viewContext,
modelExplorer,
expression,
attributes);
return attributes; If you aren't actually writing out the HTML attributes i.e. the dictionary is just for inspection, use |
Please learn me, how get the ValidationHtmlAttributesProvider from DI? |
@dougbu how do I get the _validationAttributeProvider, as you can see I have the IHtmlHelper and ModelExplorer |
@omuleanu sure, that service locator pattern is a fine fallback if your helper class isn't either itself in DI or activated. In those cases, you can just use constructor injection. |
@dougbu well the html helper is an extension method on IHtmlHelper so there's no constructor, or there's some new way doing helpers ? |
I doubt we have much guidance in this area but ASP.NET Core MVC offers a number of choices for various helper extension / addition scenarios. My recommendations
The above avoids the service locator pattern. But feel free to use that pattern if the architectural and (likely) performance downsides are 🆗 in your application. |
hi
in mvc 5 there was
html.GetUnobtrusiveValidationAttributes
in rc1 I think there was
html.GetClientValidationRules
now, I can't find anything that will return the same
I'm developing a custom helper and I need to get the client validation attributes
The text was updated successfully, but these errors were encountered: