MVC3中的自定义验证程序未显示错误消息

时间:2013-05-22 09:39:43

标签: customvalidator

我使用了链接(MVC3 unobtrusive validation group of inputs)中的上述自定义属性,但对其进行了一些修改。而不是字符串作为属性类型,我正在检查List,这是我需要的。对我来说验证工作正常。但错误消息未显示。当我调试时,我发现在要验证的属性的数据注释中设置的错误消息在自定义验证器构造函数中变为空。我尝试了来自资源包的硬编码错误消息和错误消息。

请让我知道如何解决这个问题。

我的代码供参考。

使用以下属性的属性

[AtLeastOneRequired("List1", "List2", ErrorMessage = "Test Error Message", ErrorMessageResourceType = typeof(Motorola.MWM.Web.Resources.MWMResource), ErrorMessageResourceName = "ERROR_MSG_USER_GROUP")]
public List<string> List1 { get; set; }
public List<string> List2 { get; set; }

    public class AtLeastOneRequiredAttribute : ValidationAttribute, IClientValidatable
    {


     private readonly string[] _properties;
        public AtLeastOneRequiredAttribute(params string[] properties)
        {
            _properties = properties;//properties are populated
            Console.WriteLine(ErrorMessage);//but error message is null
        }

            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (_properties == null || _properties.Length < 1)
                {
                    return null;
                }
                foreach (var property in _properties)
                {
                    var propertyInfo = validationContext.ObjectType.GetProperty(property);
                    if (propertyInfo == null)
                    {
                        return new ValidationResult(string.Format("unknown property {0}", property));
                    }

                    var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
                    if (propertyInfo.GetType().IsGenericType && (propertyValue as List<string>) != null && (propertyValue as List<string>).Count > 0)
                    {
                        return null;
                    }

                }

                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }

            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                var rule = new ModelClientValidationRule
                {
                    ErrorMessage = ErrorMessage,
                    ValidationType = "atleastonerequired"
                };
                rule.ValidationParameters["properties"] = string.Join(",", _properties);

                yield return rule;
            }
    }

先谢谢,Murali

1 个答案:

答案 0 :(得分:1)

我无法弄清楚为什么ErrorMessage将变为空。但幸运的是它现在开始工作了。现在,来自资源包的硬编码错误消息和错误消息也可以正常工作。

但是上面的代码中有一处对资源包的错误消息进行了更改。如果要从资源包中获取消息,则将由MVC填充ErrorMessage属性用户ErrorMessageString,而不是ErrorMessage属性。

[AtLeastOneRequired("List1", "List2", ErrorMessageResourceType = typeof(Motorola.MWM.Web.Resources.MWMResource),ErrorMessageResourceName = "ERROR_MSG_KEY")]
public List<string> List1 { get; set; }
public List<string> List2 { get; set; }


public override IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessageString(Instead of ErrorMessage - which should be used only for hardcoded error messages),
                ValidationType = "atleastonerequiredlist"
            };
            rule.ValidationParameters["properties"] = string.Join(",", _properties);

            yield return rule;
        }