无法显示具有自定义验证属性的ErrorMessageResourceName

我已经基于这个问题来创建一个自定义验证属性与asp.net mvc3和jquery验证:

我已经更新它来处理一组字符串属性而不是bool。 哪些工作正常。 但我的问题来了,当我用它与自定义ErrorMessage自定义消息不显示。 我无法弄清楚为什么。

[RequiredOneFromGroup("PhoneGroup", 
    ErrorMessageResourceName = "PhoneGroup", 
    ErrorMessageResourceType = typeof(WizardStrings))]
public String Mobile { get; set; } 
[RequiredOneFromGroup("PhoneGroup", 
    ErrorMessageResourceName = "PhoneGroup", 
    ErrorMessageResourceType = typeof(WizardStrings))]
public String Phone { get; set; }

以下是自定义验证属性:

[AttributeUsage(AttributeTargets.Property)]
public class RequiredOneFromGroup : ValidationAttribute, IClientValidatable
{
    public RequiredOneFromGroup(string groupName)
    {
        ErrorMessage = string.Format("You must select at least one value from group "{0}"", groupName);
        GroupName = groupName;
    }

    public string GroupName { get; private set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        foreach (var property in GetGroupProperties(validationContext.ObjectType))
        {
            var propertyValue = (string)property.GetValue(validationContext.ObjectInstance, null);
            if ( ! string.IsNullOrWhiteSpace(propertyValue))
            {
                // at least one property is true in this group => the model is valid
                return null;
            }
        }
        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }

    private IEnumerable<PropertyInfo> GetGroupProperties(Type type)
    {
        return
            from property in type.GetProperties()
            where property.PropertyType == typeof(string)
            let attributes = property.GetCustomAttributes(typeof(RequiredOneFromGroup), false).OfType<RequiredOneFromGroup>()
            where attributes.Count() > 0
            from attribute in attributes
            where attribute.GroupName == GroupName
            select property;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var groupProperties = GetGroupProperties(metadata.ContainerType).Select(p => p.Name);
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage
        };
        rule.ValidationType = string.Format("group", GroupName.ToLower());
        rule.ValidationParameters["propertynames"] = string.Join(",", groupProperties);
        yield return rule;
    }
}

我试图删除ErrorMessage的手动设置,但然后我会得到一个空的错误消息。 我如何检索我在模型的命名参数中指定的ErrorMessageResourceName的值? 我怎样才能设置它,使自定义验证属性显示它?


在您的GetClientValidationRules方法中,替换:

var rule = new ModelClientValidationRule
{
    ErrorMessage = this.ErrorMessage
};

有:

var rule = new ModelClientValidationRule
{
    ErrorMessage = FormatErrorMessage(metadata.DisplayName)
};

同时在你的构造函数中摆脱this.ErrorMessage = ...行。 您不能同时设置ErrorMessage和使用验证属性的ErrorMessageResourceNameErrorMessageResourceType属性。 这些东西是相互排斥的。


好吧,我在想,不知何故,因为自定义验证属性继承自ValidationAttribute,ErrorMessage处理将自然完成。 这似乎并非如此。

我已经解决了这个问题,如下所示:

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var groupProperties = GetGroupProperties(metadata.ContainerType).Select(p => p.Name);
        var rule = new ModelClientValidationRule
        {
            // Replaced ErrorMessage with ErrorMessageString which 
            // will hold string value of ErrorMessageResourceName if there is
            ErrorMessage = this.ErrorMessageString
        };
        rule.ValidationType = string.Format("group", GroupName.ToLower());
        rule.ValidationParameters["propertynames"] = string.Join(",", groupProperties);
        yield return rule;
    }
链接地址: http://www.djcxy.com/p/9699.html

上一篇: Unable to display ErrorMessageResourceName with custom validation attribute

下一篇: .net mvc 2 validation: summing up values of several attributes