我基于此question以使用asp.net mvc3和jquery validate创建自定义验证属性:
我已将其更新为使用一组字符串属性而不是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的值?我如何设置它以便自定义验证属性显示它?
答案 0 :(得分:5)
在GetClientValidationRules
方法替换:
var rule = new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessage
};
使用:
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName)
};
还要删除构造函数中的this.ErrorMessage = ...
行。您无法同时设置ErrorMessage并使用验证属性的ErrorMessageResourceName
和ErrorMessageResourceType
属性。那些东西是相互排斥的。
答案 1 :(得分:1)
好吧我想到了某种程度上,因为自定义验证属性继承自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;
}