1: [AttributeUsage(AttributeTargets.Class|AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
2: public abstract class ValidatorBaseAttribute : ValidationAttribute, IClientValidatable
3: {
4:
5: public string RuleName { get; set; }
6: public string MessageCategory { get; private set; }
7: public string MessageId { get; private set; }
8: public string Culture { get; set; }
9:
10: public ValidatorBaseAttribute(MessageManager messageManager, string messageCategory, string messageId, params object[] args)
11: : base(() => messageManager.FormatMessage(messageCategory, messageId, args))
12: {
13: this.MessageCategory = messageCategory;
14: this.MessageId = messageId;
15: }
16:
17: public ValidatorBaseAttribute(string messageCategory, string messageId, params object[] args)
18: : this(MessageManagerFactory.GetMessageManager(), messageCategory, messageId, args)
19: { }
20:
21: public virtual bool Match(ValidatorContext context, IEnumerable<ValidatorBaseAttribute> validators)
22: {
23: if (!string.IsNullOrEmpty(this.RuleName))
24: {
25: if (this.RuleName != context.RuleName)
26: {
27: return false;
28: }
29: }
30:
31: if (!string.IsNullOrEmpty(this.Culture))
32: {
33: if (string.Compare(this.Culture, context.Culture.Name, true) != 0)
34: {
35: return false;
36: }
37: }
38:
39: if (string.IsNullOrEmpty(this.Culture))
40: {
41: if (validators.Any(validator => validator.GetType() == this.GetType() && string.Compare(validator.Culture, context.Culture.Name, true) == 0))
42: {
43: return false;
44: }
45: }
46: return true;
47: }
48: public abstract IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context);
49: private object typeId;
50: public override object TypeId
51: {
52: get { return (null == typeId) ? (typeId = new object()) : typeId; }
53: }
54: }