Beispiel #1
0
        /// <summary>
        /// 实体对象验证
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>若有不符合要求的返回错误信息;若验证通过返回空字符串</returns>
        public static string ValidateEntity(EntityBase entity)
        {
            List <ValidateAttribute> list = GetFieldsByType(entity.GetType()).ValidateFields;//获取验证字段集合
            object fieldValue;
            string pattern;

            foreach (ValidateAttribute item in list)
            {
                fieldValue = EntityUtil.GetValueByField(entity, item.FieldName);

                if (!item.Nullable && (fieldValue == null || fieldValue.ToString() == ""))
                {
                    return(item.FieldName + "," + item.FieldDesc + (item.FieldDesc == "" ? Messages.NullableError : ""));
                }

                pattern = ValidateUtil.GetPattern(item.Expression); //尝试获取内置已存在正则表达式
                if (String.IsNullOrEmpty(pattern))                  //未获取到认为Entity配置的信息则为正则表达式
                {
                    pattern = item.Expression;
                }

                if (!String.IsNullOrEmpty(item.Expression) &&
                    !ValidateUtil.IsMatch(fieldValue.ToString(), pattern))
                {
                    return(item.FieldName + "," + item.FieldDesc + "," + Messages.ExpressionError);
                }
            }

            return("");
        }