RegularExpression Message Error Using Griffin.MvcContrib

烈酒焚心 提交于 2019-12-11 13:17:16

问题


I'm using Griffin.MvcContrib for metadata message errors validation.

I have Added a custom validation to add a regular expression validation:

protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes) {
    List<ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();

    //if (metadata.ModelType.Name == "DateTime")
    if (metadata.DataTypeName == "telephone") {
        DataAnnotationsModelValidationFactory factory;

        RegularExpressionAttribute regex = new RegularExpressionAttribute(@"^(((\+?34([ |\-])?)|(\+?\(34\)([ |\-])?))?[9|6|7]((\d{1}([ |\-])?[0-9]{3})|(\d{2}([ |\-])?[0-9]{2}))([ |\-])?[0-9]{2}([ |\-])?[0-9]{2})$");
        //regex.ErrorMessage = "Invalid date format";
        if (!AttributeFactories.TryGetValue(regex.GetType(), out factory))
            factory = DefaultAttributeFactory;

        vals.Add(factory(metadata, context, regex));
    }

    return vals.AsEnumerable();
}

In the global assax:

    ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(myResouceFile);
    ModelValidatorProviders.Providers.Clear();
    ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());
    ModelValidatorProviders.Providers.Clear();
    ModelValidatorProviders.Providers.Add(new MovilityMetadataValidatorProvider());

And in the resources i have the next keys

CommonPrompts_RegularExpression         {0} fu  
Phone_RegularExpression                 {0} fa
RegularExpression                       Incorrect Format

But it keeps showing me this message:

El campo [es-ES: Phone] debe coincidir con la expresión regular '^(((+?34([ |-])?)|(+?(34)([ |-])?))?[9|6|7]((\d{1}([ |-])?[0-9]{3})|(\d{2}([ |-])?[0-9]{2}))([ |-])?[0-9]{2}([ |-])?[0-9]{2})$'.

I have also Added the messages:

AgentDto_Phone_RegularExpression                                {0} regular 
AgentCreateEditViewModel_AgentDto_Phone_RegularExpression       {0} regular 

without any luck.

What have I to put in the Resources to change the message?

来源:https://stackoverflow.com/questions/23105025/regularexpression-message-error-using-griffin-mvccontrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!