Exemplo n.º 1
0
        /// <summary>
        /// States the abbreviation rule.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="e">The e.</param>
        /// <returns></returns>
        public static Boolean StateAbbreviationRule(Object target, RuleDescriptorBase e)
        {
            var args = e as StateAbbreviationRuleDescriptor;

            if (args == null)
            {
                throw new ArgumentException(String.Format(Resources.StringValidationRules_StateAbbreviationRule_Wrong_rule_passed_to_StateAbbreviationRule___0_FormatString, e.GetType()));
            }

            PropertyInfo objPi = target.GetType().GetProperty(args.PropertyName);

            if (!(objPi.PropertyType == typeof(String)))
            {
                throw new NotSupportedException(Resources.StringValidationRules_StateAbbreviationRule_State_abbreviation_validation_rule_can_only_be_applied_to_String_properties_FormatString);
            }

            String state = Convert.ToString(objPi.GetValue(target, null));

            if (args.RequiredEntry == RequiredEntry.Yes)
            {
                if (String.IsNullOrEmpty(state) || Convert.IsDBNull(state))
                {
                    e.BrokenRuleDescription = String.Format(Resources.StringValidationRules_BankRoutingNumberRule__0__was_null_or_empty_but_is_a_required_field_, RuleDescriptorBase.GetPropertyFriendlyName(e));
                    return(false);
                }
            }
            else
            {
                if (String.IsNullOrEmpty(state) || Convert.IsDBNull(state))
                {
                    return(true);
                }
            }

            if (StateAbbreviationValidator.CreateInstance().IsValid(state))
            {
                return(true);
            }
            args.BrokenRuleDescription = String.Format(Resources.StringValidationRules_StateAbbreviationRule_The_entered_value__0__is_not_a_valid_state_abbreviation_FormatString, state);
            return(false);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates the instance.
 /// </summary>
 /// <returns></returns>
 public static StateAbbreviationValidator CreateInstance()
 {
     return(_instance ?? (_instance = new StateAbbreviationValidator()));
 }