Example #1
0
        private static bool HasValue(object pTarget, PropertyRequiredRuleArgs pArgs)
        {
            object value = ExtendedReflectionFns.GetPropertyValue(pTarget, pArgs.RequiredPropertyInfo);

            if (value == null)
            {
                return(false);
            }
            if (value is string && ((string)value).Trim().Length == 0)
            {
                return(false);
            }
            if (value is BusinessBase && ((BusinessBase)value).IsNull)
            {
                return(false);
            }

            return(true);

            /*
             * if ( pArgs.MissingValue == null ) {
             * return true; // value is non-null and test value is null
             * } else {
             * return !pArgs.MissingValue.Equals(value);
             * }
             */
        }
Example #2
0
 public static bool ObjectRequired(object target, RuleArgs e)
 {
     if (null == ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName))
     {
         e.Description =
             string.Format(Resources.StringRequiredRule, e.DisplayPropertyName);
         return(false);
     }
     return(true);
 }
Example #3
0
        public static bool StringRequired(object target, RuleArgs e)
        {
            string value = (string)ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);

            if (string.IsNullOrEmpty(value))
            {
                e.Description =
                    string.Format(Resources.StringRequiredRule, e.DisplayPropertyName);
                return(false);
            }
            return(true);
        }
Example #4
0
        public static bool StringMinLength(object target, RuleArgs e)
        {
            int    min   = ((MinMaxArgs <Int32>)e).MinValue;
            string value = (string)ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);

            if (String.IsNullOrEmpty(value) || (value.Trim().Length < min))
            {
                e.Description = String.Format(
                    Resources.StringMinLengthRule, e.DisplayPropertyName, min.ToString());
                return(false);
            }
            return(true);
        }
Example #5
0
        public static bool StringMaxLength(object target, RuleArgs e)
        {
            int    max   = (int)((MinMaxArgs <Int32>)e).MaxValue;
            string value = (string)ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);

            if (!String.IsNullOrEmpty(value) && (value.Length > max))
            {
                e.Description = String.Format(
                    Resources.StringMaxLengthRule, e.DisplayPropertyName, max.ToString());
                return(false);
            }
            return(true);
        }
Example #6
0
        /// <summary>Rule ensuring that a numeric value is not less than the specified minimum.</summary>
        /// <typeparam name="T">Type of the property to validate.</typeparam>
        /// <param name="target">Object containing value to validate.</param>
        /// <param name="e">Arguments variable specifying the name of the property to validate,
        /// along with the min allowed value.</param>
        /// <remarks>
        /// Converts the arguments to the target type, T, if it can and calls CompareTo.
        /// <para>Heavily revised by IdeaBlade.</para>
        /// </remarks>
        public static bool MinValue <T>(object target, RuleArgs e)
        {
            T      min    = (T)((MinMaxArgs <T>)e).MinValue;
            object value  = ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);
            bool   result = Compare <T>(value, min) >= 0;

            if (!result)
            {
                e.Description =
                    string.Format(Resources.MinValueRule, e.DisplayPropertyName, min.ToString());
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #7
0
        public static bool MinMaxValue <T>(object target, RuleArgs e)
        {
            T      min   = ((MinMaxArgs <T>)e).MinValue;
            T      max   = ((MinMaxArgs <T>)e).MaxValue;
            object value = ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);
            bool   ok    = true;

            if (value != null)
            {
                ok = (Compare <T>(value, min) >= 0 && Compare <T>(value, max) <= 0);
            }
            if (ok)
            {
                return(true);
            }
            e.Description = String.Format(
                Resources.BetweenRule, e.DisplayPropertyName, min.ToString(), max.ToString());
            return(false);
        }
Example #8
0
        public static bool IntegerMinMaxValue(object target, RuleArgs e)
        {
            long   min   = ((MinMaxArgs <Int64>)e).MinValue;
            long   max   = ((MinMaxArgs <Int64>)e).MaxValue;
            object value = ExtendedReflectionFns.GetPropertyValue(target, e.PropertyName);
            bool   ok    = true;

            if (value != null)
            {
                long testValue = Convert.ToInt64(value);
                ok = (testValue >= min) && (testValue <= max);
            }
            if (ok)
            {
                return(true);
            }
            e.Description = String.Format(
                Resources.BetweenRule, e.DisplayPropertyName, min.ToString(), max.ToString());
            return(false);
        }
Example #9
0
        /// <summary>Rule that check that value matches a given regex pattern.</summary>
        /// <param name="target">Object containing the data to validate</param>
        /// <param name="e">RegExRuleArgs parameter specifying the name of the
        /// property to validate and the regex pattern.</param>
        /// <returns>False if the rule is broken</returns>
        /// <remarks>This implementation uses late binding.</remarks>
        public static bool RegExMatch(object target, RuleArgs e)
        {
            RegExRuleArgs args  = (RegExRuleArgs)e;
            Regex         rx    = args.RegEx;
            string        value = (string)ExtendedReflectionFns.GetPropertyValue(target, args.PropertyName);

            // Contra CSLA, rule does NOT fail if there is no entry;
            // That test is the job of a "PropertyRequired" rule.
            if (value == null || value.Trim() == String.Empty)
            {
                return(true);
            }
            if (!rx.IsMatch(value))
            {
                args.Description =
                    String.Format(args.MessageTemplate, args.DisplayPropertyName);
                return(false);
            }
            else
            {
                return(true);
            }
        }