public override bool IsValid(ValidationContext context) { if (context.IsNull(context)) return true; Match match = Validator.Match(context.Value.ToString()); return match.Success && match.Value == context.Value.ToString(); }
private static bool IsNullInternal(ValidationContext context) { if (context.Value == null) return true; if (context.NullValue is DBNull) return false; return context.NullValue.Equals(context.Value); }
public override bool IsValid(ValidationContext context) { if (context.IsNull(context)) return true; DateTime contextValue = Convert.ToDateTime(context.Value); DateTime testValue = (DateTime)GetValue(context); return testValue > contextValue || !IsExclusive && testValue == contextValue; }
public static bool IsValid(ValidationContext context, string fieldName) { ValidatorBaseAttribute[] attrs = null; object value = null; #if !SILVERLIGHT if (context.PropertyDescriptor != null) { value = context.PropertyDescriptor.GetValue(context.Object); List<ValidatorBaseAttribute> list = null; foreach (object o in context.PropertyDescriptor.Attributes) { if (o is ValidatorBaseAttribute) { if (list == null) list = new List<ValidatorBaseAttribute>(); list.Add((ValidatorBaseAttribute)o); } } if (list != null) attrs = list.ToArray(); } else #endif { context.MemberAccessor = context.TypeAccessor[fieldName]; if (context.MemberAccessor != null) { value = context.MemberAccessor.GetValue(context.Object); attrs = context.MemberAccessor.GetAttributes<ValidatorBaseAttribute>(); } } if (attrs != null) { context.Value = value; for (int i = 0; i < attrs.Length; i++) { if (!attrs[i].IsValid(context)) return false; } } return true; }
public static ValidationContext InitContext( ValidationContext context, object obj, PropertyDescriptor pd, ValidationContext.IsNullHandler isNull) { if (context == null) context = new ValidationContext(); context.Object = obj; context.IsNull = isNull ?? new ValidationContext.IsNullHandler(IsNullInternal); context.PropertyDescriptor = pd; return context; }
public static void Validate(ValidationContext context) { foreach (MemberAccessor ma in context.TypeAccessor) { ValidatorBaseAttribute[] attrs = ma.GetAttributes<ValidatorBaseAttribute>(); if (attrs == null) continue; context.MemberAccessor = ma; context.Value = ma.GetValue(context.Object); for (int i = 0; i < attrs.Length; i++) { ValidatorBaseAttribute attr = attrs[i]; if (attr.IsValid(context) == false) throw new ValidationException(attr.GetErrorMessage(context)); } } }
protected virtual string GetPropertyFriendlyName(ValidationContext context) { MemberInfo mi = context.MemberInfo; string className = mi.DeclaringType.Name; string fieldName = mi.Name; // Get class name. // object[] attrs = mi.DeclaringType.GetCustomAttributes(typeof(FriendlyNameAttribute), true); if (attrs.Length > 0) className = ((FriendlyNameAttribute)attrs[0]).Name; else { attrs = mi.DeclaringType.GetCustomAttributes(typeof(DisplayNameAttribute), true); if (attrs.Length > 0) className = ((DisplayNameAttribute)attrs[0]).DisplayName; } // Get field name. // attrs = mi.GetCustomAttributes(typeof(FriendlyNameAttribute), true); if (attrs.Length > 0) fieldName = ((FriendlyNameAttribute)attrs[0]).Name; else { attrs = mi.GetCustomAttributes(typeof(DisplayNameAttribute), true); if (attrs.Length > 0) fieldName = ((DisplayNameAttribute)attrs[0]).DisplayName; } return string.IsNullOrEmpty(className)? fieldName: className + "." + fieldName; }
public abstract bool IsValid(ValidationContext context);
public static void Validate(object obj, ValidationContext.IsNullHandler isNull) { Validate(InitContext(null, obj, null, isNull)); }
public static string[] GetErrorMessages(object obj, PropertyDescriptor pd, ValidationContext.IsNullHandler isNull) { return GetErrorMessages(InitContext(null, obj, pd, isNull), pd.Name); }
public static string[] GetErrorMessages( object obj, string fieldName, ValidationContext.IsNullHandler isNull) { return GetErrorMessages(InitContext(null, obj, null, isNull), fieldName); }
public static string[] GetErrorMessages(ValidationContext context, string fieldName) { context.MemberAccessor = context.TypeAccessor[fieldName]; if (context.MemberAccessor != null) { List<string> messages = new List<string>(); ValidatorBaseAttribute[] attrs = context.MemberAccessor.GetAttributes<ValidatorBaseAttribute>(); if (attrs != null) { context.Value = context.MemberAccessor.GetValue(context.Object); for (int i = 0; i < attrs.Length; i++) messages.Add(attrs[i].GetErrorMessage(context)); return messages.ToArray(); } } return new string[0]; }
public static bool IsValid(object obj, PropertyDescriptor pd, ValidationContext.IsNullHandler isNull) { return IsValid(InitContext(null, obj, pd, isNull), pd.Name); }
public override bool IsValid(ValidationContext context) { return context.IsNull(context) || context.Value.ToString().Length <= _value; }
public virtual string GetErrorMessage(ValidationContext context) { return string.Format(ErrorMessage, GetPropertyFriendlyName(context)); }
public override bool IsValid(ValidationContext context) { if (context.IsNull(context)) return true; object contextValue = context.Value; object testValue = GetValue(context); if (contextValue is Int32) { Int32 tv = Convert.ToInt32(testValue); return tv > (Int32)contextValue || !IsExclusive && tv == (Int32)contextValue; } if (contextValue is decimal) { decimal tv = Convert.ToDecimal(testValue); return tv > (decimal)contextValue || !IsExclusive && tv == (decimal)contextValue; } if (contextValue is double) { double tv = Convert.ToDouble(testValue); return tv > (double)contextValue || !IsExclusive && tv == (double)contextValue; } if (contextValue is float) { float tv = Convert.ToSingle(testValue); return tv > (float)contextValue || !IsExclusive && tv == (float)contextValue; } if (contextValue is byte) { byte tv = Convert.ToByte(testValue); return tv > (byte)contextValue || !IsExclusive && tv == (byte)contextValue; } if (contextValue is char) { char tv = Convert.ToChar(testValue); return tv > (char)contextValue || !IsExclusive && tv == (char)contextValue; } if (contextValue is Int16) { Int16 tv = Convert.ToInt16(testValue); return tv > (Int16)contextValue || !IsExclusive && tv == (Int16)contextValue; } if (contextValue is sbyte) { sbyte tv = Convert.ToSByte(testValue); return tv > (sbyte)contextValue || !IsExclusive && tv == (sbyte)contextValue; } if (contextValue is UInt16) { UInt16 tv = Convert.ToUInt16(testValue); return tv > (UInt16)contextValue || !IsExclusive && tv == (UInt16)contextValue; } if (contextValue is UInt32) { UInt32 tv = Convert.ToUInt32(testValue); return tv > (UInt32)contextValue || !IsExclusive && tv == (UInt32)contextValue; } if (contextValue is Int64) { Int64 tv = Convert.ToInt64(testValue); return tv > (Int64)contextValue || !IsExclusive && tv == (Int64)contextValue; } if (contextValue is UInt64) { UInt64 tv = Convert.ToUInt64(testValue); return tv > (UInt64)contextValue || !IsExclusive && tv == (UInt64)contextValue; } return true; }
public virtual object GetValue(ValidationContext context) { return _value; }
public override string GetErrorMessage(ValidationContext context) { return string.Format(ErrorMessage, GetPropertyFriendlyName(context), GetValue(context), IsExclusive? " exclusive": string.Empty); }
public override string GetErrorMessage(ValidationContext context) { return string.Format(ErrorMessage, GetPropertyFriendlyName(context), Value); }
public override bool IsValid(ValidationContext context) { return context.IsNull(context) == false; }
public static bool IsValid(object obj, string fieldName, ValidationContext.IsNullHandler isNull) { return IsValid(InitContext(null, obj, null, isNull), fieldName); }