Exemplo n.º 1
0
        /// <summary>
        /// 添加验证类型
        /// </summary>
        /// <param name="node"></param>
        /// <param name="propertyRules"></param>
        /// <param name="rules"></param>
        protected virtual void AddPropertyRulesByXmlNode(XmlNode node, IList <RuleInfo> propertyRules,
                                                         IDictionary <string, RuleInfo> rules)
        {
            if (node.Attributes == null || !rules.ContainsKey(node.Attributes["RuleName"].Value))
            {
                return;
            }
            var rule = new RuleInfo();
            var args = GetPropertyRuleArgsByXmlNode(node);

            rule.ValidationTypes = node.Attributes["ValidationType"] != null
                                       ? GetValidationTypesByString(node.Attributes["ValidationType"].Value)
                                       : GetDefaultValidationTypes();

            rule.Pattern     = ReplacePatternArgs(rules[node.Attributes["RuleName"].Value].Pattern, args);
            rule.IsRange     = rules[node.Attributes["RuleName"].Value].IsRange;
            rule.IsMultiline = node.Attributes["IsMultiline"] != null &&
                               "true".Equals(node.Attributes["IsMultiline"].Value);
            rule.IsIgnoreCase = node.Attributes["IsIgnoreCase"] != null &&
                                "true".Equals(node.Attributes["IsIgnoreCase"].Value);
            rule.Message = node.Attributes["Message"] != null ? node.Attributes["Message"].Value : null;
            propertyRules.Add(rule);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加错误信息
        /// </summary>
        /// <param name="results"></param>
        /// <param name="validInfo"></param>
        /// <param name="value"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        protected virtual void AddErrorInfo(IList <ErrorInfo> results, ValidationInfo validInfo, object value, RuleInfo rule)
        {
            var isValidate = true;

            if (rule.IsRange)
            {
                object vres = null;
                if (value != null)
                {
                    var reg = new Regex(@"[^\d]");
                    vres = reg.Replace(value.ToString(), "");
                }
                if (vres == null || vres.ToString() == "")
                {
                    vres = TryConvertValue(value, typeof(int)) ?? 0;
                }
                value = vres;
                var    values     = rule.Pattern.Split('-');
                object startValue = values.Length >= 1 ? values[0] : null;
                object endValue   = values.Length >= 2 ? values[1] : null;
                if (startValue != null)
                {
                    isValidate = Convert.ToDouble(value) >= Convert.ToDouble(startValue);
                }
                if (endValue != null)
                {
                    isValidate = isValidate && Convert.ToDouble(value) <= Convert.ToDouble(endValue);
                }
            }
            else
            {
                var revValue = value == null ? "" : value.ToString();
                var option   = rule.IsMultiline ? RegexOptions.Multiline : RegexOptions.Singleline;
                option = rule.IsIgnoreCase ? option & RegexOptions.IgnoreCase : option;
                if (!Regex.IsMatch(revValue, rule.Pattern, option))
                {
                    isValidate = false;
                }
            }
            if (!isValidate)
            {
                var message = string.IsNullOrEmpty(rule.Message) ? validInfo.Message : rule.Message;
                var rev     = new ErrorInfo {
                    Key = validInfo.PropertName, Message = message
                };
                results.Add(rev);
            }
        }