Ejemplo n.º 1
0
        /// <summary>
        /// string 长度控制
        /// </summary>
        /// <param name="field"></param>
        /// <param name="fieldName"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool LengthControl(string field, string fieldName, int minLength, int maxLength, ref ValidateResult result)
        {
            if (!Required(field, fieldName, ref result))
            {
                return(false);
            }

            if (minLength >= maxLength)
            {
                throw new ArgumentException(string.Format("minLength {0} > maxLength {1}", minLength, maxLength));
            }


            int length = field.Length;

            if (length > maxLength || length < minLength)
            {
                result.AddError(string.Format("{0} 长度不在范围内({1} ~ {2})", fieldName, minLength, maxLength));
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 唯一性验证
        /// </summary>
        /// <param name="model"></param>
        /// <param name="fieldName"></param>
        /// <param name="CheckNameUnique">委托, 传入一个验证model是否唯一的方法</param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool UniqueField(object model, string fieldName, Predicate <object> CheckNameUnique, ref ValidateResult result)
        {
            if (CheckNameUnique == null)
            {
                throw new ArgumentException("CheckNameUnique委托不能为空!");
            }
            if (!CheckNameUnique(model))
            {
                result.AddError(string.Format("{0} 值已存在,不能重复 ", fieldName));
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
 public virtual ValidateResult Validate(T data)
 {
     return(ValidateResult.CreateNormal());
 }