Esempio n. 1
0
        /// <summary>
        /// Rule ensuring a String value contains one or more
        /// characters.
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <remarks>
        /// This implementation uses late binding, and will only work
        /// against String property values.
        /// </remarks>
        public static bool StringRequired(object target, ValidationRuleArgs e)
        {
            PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

            if (p != null)
            {
                string value = (string)p.GetValue(target, null);
                if (string.IsNullOrEmpty(value))
                {
                    if (string.IsNullOrEmpty(e.Description))
                    {
                        e.Description = e.FriendlyName + " is required.";
                    }
                    return(false);
                }
                return(true);
            }
            else
            {
                throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Rule that does not allow a property value to be null
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <returns>Returns true if the property value is not null; false otherwise.</returns>
        public static bool NotNull(object target, ValidationRuleArgs e)
        {
            PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

            if (p != null)
            {
                object value = p.GetValue(target, null);

                if (value == null)
                {
                    if (string.IsNullOrEmpty(e.Description))
                    {
                        e.Description = string.Format("{0} cannot be null.", e.FriendlyName);
                    }
                    return(false);
                }

                return(true);
            }
            else
            {
                throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Generic rule that determines if an object's property is greater than or equal to a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool GreaterThanOrEqualToValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.GreaterThanOrEqualTo));
 }
Esempio n. 4
0
 /// <summary>
 /// Generic rule that determines if an object's property is equal to a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool EqualsValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.EqualTo));
 }
Esempio n. 5
0
 /// <summary>
 /// Generic rule that determines if an object's property is less than a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool LessThanValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.LessThan));
 }
Esempio n. 6
0
 /// <summary>
 /// Adds a rule to the list of validated rules.
 /// </summary>
 /// <remarks>
 /// </remarks>
 /// <param name="handler">The method that implements the rule.</param>
 /// <param name="args">
 /// A <see cref="Validation.ValidationRuleArgs"/> object specifying the property name and other arguments
 /// passed to the rule method
 /// </param>
 public void AddValidationRuleHandler(Validation.ValidationRuleHandler handler, Validation.ValidationRuleArgs args)
 {
     ValidationRules.AddRule(handler, args);
 }