Esempio n. 1
0
        public override bool IsValidation(Property source)
        {
            if (!IsTypeOf())
            {
                this.Message = $"不是{typeof(T).Name}的有效值";

                return(false);
            }

            this.Flag = source.Required != null ? "*" : "";

            string error = null;

            ////  Do :检查总体
            //if (!source.IsValid(this.Text, this.DisplayName, out error))
            //{
            //    this.Message = error;
            //    return false;
            //}

            //  Do :检查总体
            var items = this.Text?.Split(splitC).ToList();

            foreach (var item in source.GetValidations())
            {
                //  Do :长度范围特殊处理,取数组的长度
                if (item.GetType() == typeof(Range))
                {
                    if (!item.IsValid(items?.Count))
                    {
                        this.Message = item.ErrorMessage ?? item.FormatErrorMessage(this.DisplayName);

                        return(false);
                    }
                }
                else
                {
                    if (!item.IsValid(this.Text))
                    {
                        this.Message = item.ErrorMessage ?? item.FormatErrorMessage(this.DisplayName);

                        return(false);
                    }
                }
            }

            //  Do :检查子项
            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];

                if (!source.ItemValidations.IsValid(item, $"索引[{i.ToString()}] 值'{item}'", out error))
                {
                    this.Message = error;
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// 用配置文件验证是否有效
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public override bool IsValidation(Property source)
        {
            List <Validation> result = source.GetValidations();

            this.Flag = source.Required != null ? "*" : "";

            foreach (var r in result)
            {
                if (!r.IsValid(this.BeforeValid(this.Text)))
                {
                    this.Message = r.ErrorMessage ?? r.FormatErrorMessage(this.DisplayName);

                    //  Do :保存实体验证失败
                    return(false);
                }
            }

            return(base.IsValidation(source));
        }