public static void ValidateValue(object value, ValidationContext validationContext, IEnumerable<ValidationAttribute> validationAttributes) { if (validationContext == null) throw new ArgumentNullException("validationContext"); var validationResults = new List<ValidationResult>(); if (TryValidateValue(value, validationContext, validationResults, validationAttributes)) return; ValidationResult result = validationResults.Count > 0 ? validationResults[0] : null; throw new ValidationException(result, null, value); }
public static bool TryValidateValue(object value, ValidationContext validationContext, ICollection<ValidationResult> validationResults, IEnumerable<ValidationAttribute> validationAttributes) { if (validationContext == null) throw new ArgumentNullException("validationContext"); ValidationResult result; // It appears .NET makes this call before checking whether // validationAttributes is null... ValidationAttribute vattr = validationAttributes.FirstOrDefault<ValidationAttribute>(attr => attr is RequiredAttribute); if (vattr != null) { result = vattr.GetValidationResult(value, validationContext); if (result != ValidationResult.Success) { if (validationResults != null) validationResults.Add(result); return false; } } if (validationAttributes == null) return true; bool valid = true; foreach (ValidationAttribute attr in validationAttributes) { if (attr == null || (attr is RequiredAttribute)) continue; result = attr.GetValidationResult(value, validationContext); if (result != ValidationResult.Success) { valid = false; if (validationResults != null) validationResults.Add(result); } } return valid; }
/* *RGE* public static void ValidateObject(object instance, ValidationContext validationContext) { ValidateObject(instance, validationContext, false); }*/ /* *RGE* public static void ValidateObject(object instance, ValidationContext validationContext, bool validateAllProperties) { if (instance == null) throw new ArgumentNullException("instance"); if (validationContext == null) throw new ArgumentNullException("validationContext"); var validationResults = new List<ValidationResult>(); if (TryValidateObject(instance, validationContext, validationResults, validateAllProperties)) return; ValidationResult result = validationResults.Count > 0 ? validationResults[0] : null; throw new ValidationException(result, null, instance); }*/ public static void ValidateProperty(object value, ValidationContext validationContext) { if (validationContext == null) throw new ArgumentNullException("validationContext"); var validationResults = new List<ValidationResult>(); if (TryValidateProperty(value, validationContext, validationResults)) return; ValidationResult result = validationResults.Count > 0 ? validationResults[0] : null; throw new ValidationException(result, null, value); }
/* *RGE* public static bool TryValidateObject(object instance, ValidationContext validationContext, ICollection<ValidationResult> validationResults) { return TryValidateObject(instance, validationContext, validationResults, false); } public static bool TryValidateObject(object instance, ValidationContext validationContext, ICollection<ValidationResult> validationResults, bool validateAllProperties) { if (instance == null) throw new ArgumentNullException("instance"); if (validationContext == null) throw new ArgumentNullException("validationContext"); if (!Object.ReferenceEquals(instance, validationContext.ObjectInstance)) throw new ArgumentException("The instance provided must match the ObjectInstance on the ValidationContext supplied.", "instance"); bool valid = true; Type instanceType = instance.GetType(); TypeDescriptor.GetAttributes(instanceType).Validate<ValidationAttribute>(instance, validationContext, validationResults, ref valid); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance); if (properties != PropertyDescriptorCollection.Empty && properties.Count > 0) { foreach (PropertyDescriptor pdesc in properties) { object value = pdesc.GetValue(instance); ValidateProperty(pdesc, value, validationContext, validationResults, validateAllProperties, ref valid); } } return valid; } */ /* *RGE* static void ValidateProperty(PropertyDescriptor pdesc, object value, ValidationContext validationContext, ICollection<ValidationResult> validationResults, bool validateAll, ref bool valid) { AttributeCollection attributes = pdesc.Attributes; attributes.Validate<RequiredAttribute>(value, validationContext, validationResults, ref valid); if (validateAll) attributes.ValidateExcept<RequiredAttribute>(value, validationContext, validationResults, ref valid); }*/ /* *RGE* static PropertyDescriptor GetProperty(Type type, string propertyName, object value) { if (String.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName"); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(type); PropertyDescriptor pdesc = null; if (properties != PropertyDescriptorCollection.Empty && properties.Count > 0) pdesc = properties.Find(propertyName, false); if (pdesc == null) throw new ArgumentException(String.Format("The type '{0}' does not contain a public property named '{1}'.", type.Name, propertyName), "propertyName"); Type valueType = value == null ? null : value.GetType(); Type propertyType = pdesc.PropertyType; bool invalidType = false; Console.WriteLine("valueType == {0}; propertyType == {1} (reference? {2})", valueType == null ? "<null>" : valueType.FullName, propertyType, !propertyType.IsValueType || (Nullable.GetUnderlyingType(propertyType) != null)); if (valueType == null) invalidType = !(!propertyType.IsValueType || (Nullable.GetUnderlyingType(propertyType) != null)); else if (propertyType != valueType) invalidType = true; if (invalidType) throw new ArgumentException(String.Format("The value of property '{0}' must be of type '{1}'.", propertyName, type.FullName), "propertyName"); return pdesc; }*/ /* *RGE* public static bool TryValidateProperty(object value, ValidationContext validationContext, ICollection<ValidationResult> validationResults) { // LAMESPEC: value can be null, validationContext must not if (validationContext == null) throw new ArgumentNullException("validationContext"); PropertyDescriptor pdesc = GetProperty(validationContext.ObjectType, validationContext.MemberName, value); if (value == null) return true; bool valid = true; ValidateProperty(pdesc, value, validationContext, validationResults, true, ref valid); return valid; }*/ /// <summary> /// *RGE* Il s'agit de mon implémentation simplifié. /// </summary> /// <param name="value"></param> /// <param name="validationContext"></param> /// <param name="validationResults"></param> /// <returns></returns> public static bool TryValidateProperty(object value, ValidationContext validationContext, ICollection<ValidationResult> validationResults) { if (validationContext == null) throw new ArgumentNullException("validationContext"); if (value == null) return true; PropertyInfo[] pis = validationContext.GetType().GetProperties(); foreach (PropertyInfo pi in pis) { if (pi.Name == validationContext.MemberName) { foreach (object o in pi.GetCustomAttributes(false)) { if (o.GetType() == typeof(RequiredAttribute)) { try { (o as RequiredAttribute).Validate(value, o.GetType().ToString()); } catch (ValidationException ex) { ValidationResult result = new ValidationResult("Le champ est requis"); validationResults.Add(result); } } else if (o.GetType() == typeof(StringLengthAttribute)) { try { (o as StringLengthAttribute).Validate(value, o.GetType().ToString()); } catch (ValidationException ex) { ValidationResult result = new ValidationResult("Le nombre de caractère saisi est trop long."); validationResults.Add(result); } } } } } bool valid = (validationResults.Count > 0) ? false : true; return valid; }