コード例 #1
0
ファイル: ValidationResult.cs プロジェクト: Rakoun/librometer
 protected ValidationResult(ValidationResult validationResult)
 {
 }
コード例 #2
0
ファイル: Validator.cs プロジェクト: Rakoun/librometer
        /* *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;
        }