-
Notifications
You must be signed in to change notification settings - Fork 2.1k
How to get a validation attr from model by property name? #5240
Comments
What you've got will work. We're planning to address #5028 in 1.1.0. This bug appears to duplicate that request. Please reopen if you disagree. |
OP in #5240 provided his workaround for pre-1.1.0. |
Oops, comment was meant for #5028. |
I have been trying to get this to work, without success. I am trying to get a list of validators using the following code, but this method does not seem to exist. IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers.GetValidators(modelMetaData, this.ViewContext); I think the problem is that I am trying to do this server side instead of in a cshtml file, so I can not use Should I instead be using |
Yes. Well, the I spoke too quickly when I said "What you've got will work." The general idea is fine but the details look like an earlier release. The current var clientValidatorProviders = optionsAccessor.Value.ClientModelValidatorProviders;
_clientModelValidatorProvider = new CompositeClientModelValidatorProvider(clientValidatorProviders);
// ...
var validators = _clientValidatorCache.GetValidators(modelExplorer.Metadata, _clientModelValidatorProvider);
if (validators.Count > 0)
{
var validationContext = new ClientModelValidationContext(
viewContext,
modelExplorer.Metadata,
_metadataProvider,
tagBuilder.Attributes);
for (var i = 0; i < validators.Count; i++)
{
var validator = validators[i];
validator.AddValidation(validationContext);
}
} You can do the same thing w/ a |
this is what I do right now:
|
Please give me more detailed information about a workaround. I can not find his in #5240 |
@jsdmitry I just did ^ |
Our html helpers generate the input tag on the client side and so "data-" attributes are not applied to the input element. For this reason, I cannot use your workaround. |
Thank you very much guys. Obviously I'm still getting familiar with the new framework. This is perfect. |
Yes, I can not use @omuleanu workaround either, as the validation data attributes are not yet applied to the input element. I'm working with the code provided by @dougbu and I think I can make this work. It's unfortunate because I want to use this in a static class, so I can not take advantage of dependency injection to get access to some of those classes. But will work with what I have. Thanks for your all your help. |
I use the validator.GetClientValidationRules() method that returns validation parameters from the ModelClientValidationRule class. And, It works perfectly for all validation attributes, but sadly not in the ASP.Net Core. Is there some public API I could use to get validation parameters from a validation attribute in ASP.Net Core?
My model:
This is my method that generates my client validation rules by the property name of the model:
The text was updated successfully, but these errors were encountered: