Comments (7)
I've found a way to let it work.
in your Application_Start don't clear ModelValidatorProviders.provoders, and don't add LocalizedModelValidatorProvider to Providers collection, just set the ModelMetadataProviders.Current to LocalizedModelMetadataProvider.
The follow example code will be called by Application_Start() in Global.asax.cs
private void SetupLocalizationProviders()
{
//ModelValidatorProviders.Providers.Clear(); // Don't execute this for customized DataAnnotation work
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();
//ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider()); // Don't execute this for customized DataAnnotation work
}
from griffin.mvccontrib.
Hi,
I tried your fix but actually the localization is not working anymore. Just the default texts are selected, not the ones from the database.
Is there a real fix how to get the SQL Server Provider working with Client Validation?
from griffin.mvccontrib.
The code is available here: https://github.com/jgauffin/griffin.mvccontrib/blob/master/source/Griffin.MvcContrib/Localization/LocalizedModelValidatorProvider.cs
Feel free to try to provide a fix.
from griffin.mvccontrib.
Hi jgauffin,
I noticed in your code the class ValidationAttributeAdapterFactory constructor calls MapDefaultRules(), only init the built-in Model Client Validation Rules. can you provide a way to add more client validation rules for custom attributes?
from griffin.mvccontrib.
step 1: modify the class LocalizedModelValidatorProvider, add a property as following code:
public ValidationAttributeAdapterFactory ClientValidationRuleFactory
{
get
{
return _adapterFactory;
}
}
step 2: add helper class in your MVC project to extend the client validation rules, the following example code is for DataAnnotationsExtensions and MVC Foolproof Validation:
public static class ClientValidationRuleFactoryHelper
{
public static void MapClientValidationRules(ValidationAttributeAdapterFactory factory)
{
MapExtensionClientValidationRules(factory);
MapFoolProofClientValidationRules(factory);
}
private static void MapExtensionClientValidationRules(ValidationAttributeAdapterFactory factory)
{
AddDelegateRule<EmailAttribute>(factory, (attribute, errMsg) =>
{
return new[]
{
new ModelClientValidationEmailRule(errMsg)
};
});
AddDelegateRule<MaxAttribute>(factory, (attribute, errMsg) =>
{
var attr = (MaxAttribute)attribute;
return new[]
{
new ModelClientValidationMaxRule(errMsg, attr.Max)
};
});
// ---- other attributes client validation rules
}
private static void MapFoolProofClientValidationRules(ValidationAttributeAdapterFactory factory)
{
AddDelegateRule<GreaterThanAttribute>(factory, (attribute, errMsg) =>
{
var attr = (ContingentValidationAttribute)attribute;
var rule = new ModelClientValidationRule() { ErrorMessage = errMsg, ValidationType = attr.ClientTypeName.ToLower() };
foreach (var pair in attr.ClientValidationParameters)
{
rule.ValidationParameters[pair.Key] = pair.Value;
}
return new[]
{
rule
};
});
// ---- other attributes client validation rules
}
private static void AddDelegateRule<T>(ValidationAttributeAdapterFactory factory,
Func<ValidationAttribute, string, IEnumerable<ModelClientValidationRule>> func)
where T : ValidationAttribute
{
factory.Map<T>(new DelegateValidationAttributeAdapterFactory(func));
}
}
Step 3: in Global.asax.cs file:
private void SetupLocalizationProviders()
{
var provider = ModelValidatorProviders.Providers.Where(p => p.GetType() == typeof(DataAnnotationsModelValidatorProvider)).FirstOrDefault();
if (provider != null)
{
ModelValidatorProviders.Providers.Remove(provider);
}
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();
var localProvider = new LocalizedModelValidatorProvider();
ModelValidatorProviders.Providers.Add(localProvider);
//Register Client Validation Rules
ClientValidationRuleFactoryHelper.MapClientValidationRules(localProvider.ClientValidationRuleFactory);
}
from griffin.mvccontrib.
thank you. :) I'll merge that code asap
from griffin.mvccontrib.
Hi,
The griffin.MvcContrib is a clean solution for localization :)
I have the same problem with foolproof and DataAnnotationExtensions.
When can I get the new version?
Thank you!
from griffin.mvccontrib.
Related Issues (20)
- LocalizedModelValidatorProvider does not contain a constructor that takes 1 arguments HOT 6
- Using EmbeddedFileProvider for plugin css/js files HOT 6
- System.Web.Mvc.CompareAttribute not working HOT 1
- RavenDB localization provider setup HOT 12
- Overwritting of texts in RavenDB on site refresh HOT 1
- RemoteAttribute Unobstrusive Text HOT 3
- Admin section - edit, delete icon do not show up HOT 2
- Attribute not recognized (MVC4): System.ComponentModel.DataAnnotations.EmailAddressAttribute : DataTypeAttribute HOT 5
- Problem with select html helper HOT 4
- Error HOT 1
- Caching of the ViewLocalizer HOT 2
- FileLocalizedTypeRepository loads json file for every translation HOT 2
- Localizing text for ModelState.AddModelError()
- data-val-number attribute is missing HOT 1
- Caching of RepositoryStringProvider results
- GriffinCaching : Closed Connection
- Web API support?
- No constructor for LocalizedModelValidatorProvider takes 1 argument
- Pull request for update to MVC5 and AppVeyor HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from griffin.mvccontrib.