예제 #1
0
        protected override void Validate(Entity entity, RuleArgs e)
        {
            var value = Convert.ToDouble(entity.GetProperty(e.Property));

            var min = this.Min;
            if (value < min)
            {
                if (this.MessageBuilder != null)
                {
                    e.BrokenDescription = this.MessageBuilder(entity);
                }
                else
                {
                    e.BrokenDescription = string.Format("{0} 不能低于 {1}。".Translate(), e.DisplayProperty(), min);
                }
            }
            else
            {
                var max = this.Max;
                if (value > max)
                {
                    if (this.MessageBuilder != null)
                    {
                        e.BrokenDescription = this.MessageBuilder(entity);
                    }
                    else
                    {
                        e.BrokenDescription = string.Format("{0} 不能超过 {1}。".Translate(), e.DisplayProperty(), max);
                    }
                }
            }
        }
예제 #2
0
        protected override void Validate(Entity entity, RuleArgs e)
        {
            var value = Convert.ToDouble(entity.GetProperty(e.Property));

            if (value <= 0)
            {
                if (this.MessageBuilder != null)
                {
                    e.BrokenDescription = this.MessageBuilder(entity);
                }
                else
                {
                    e.BrokenDescription = string.Format("{0} 需要是正数。".Translate(), e.DisplayProperty());
                }
            }
        }
예제 #3
0
        protected override void Validate(Entity entity, RuleArgs e)
        {
            var id = entity.Id;

            var repo = RF.Find(ReferenceProperty.Owner);
            if (repo != null)
            {
                var criteria = new CommonQueryCriteria();
                criteria.Add(ReferenceProperty.Property, id);

                var count = repo.CountBy(criteria);
                if (count > 0)
                {
                    e.BrokenDescription = this.BuildError(entity, count);
                }
            }
        }
예제 #4
0
 protected override void Validate(Entity entity, RuleArgs e)
 {
     var value = entity.GetProperty(e.Property) as string;
     if (!string.IsNullOrEmpty(value))
     {
         var min = this.Min;
         if (value.Length < min)
         {
             if (this.MessageBuilder != null)
             {
                 e.BrokenDescription = this.MessageBuilder(entity);
             }
             else
             {
                 e.BrokenDescription = string.Format(
                     "{0} 不能低于 {1} 个字符。".Translate(),
                     e.DisplayProperty(), min
                     );
             }
         }
         else
         {
             var max = this.Max;
             if (value.Length > max)
             {
                 if (this.MessageBuilder != null)
                 {
                     e.BrokenDescription = this.MessageBuilder(entity);
                 }
                 else
                 {
                     e.BrokenDescription = string.Format(
                         "{0} 不能超过 {1} 个字符。".Translate(),
                         e.DisplayProperty(), max
                         );
                 }
             }
         }
     }
 }
예제 #5
0
        /// <summary>
        /// 限制实体的某一个或几个属性的值在数据库中不存在的规则。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="e"></param>
        protected override void Validate(Entity entity, RuleArgs e)
        {
            if (entity.PersistenceStatus == PersistenceStatus.New ||
                entity.PersistenceStatus == PersistenceStatus.Modified)
            {
                this.InitProperties(e);

                var criteria = new CommonQueryCriteria();
                bool hasValue = this.AddToCriteria(entity, criteria);

                if (hasValue)
                {
                    //查询实体的个数,如果已经存在,则构造错误信息
                    var repo = entity.GetRepository();
                    var count = repo.CountBy(criteria);
                    if (count > 0)
                    {
                        e.BrokenDescription = BuildError(entity);
                    }
                }
            }
        }
예제 #6
0
        protected override void Validate(Entity entity, RuleArgs e)
        {
            var value = (string)entity.GetProperty(e.Property) ?? string.Empty;

            if (!string.IsNullOrEmpty(value))
            {
                if (!this.Regex.IsMatch(value))
                {
                    if (this.MessageBuilder != null)
                    {
                        e.BrokenDescription = this.MessageBuilder(entity);
                    }
                    else
                    {
                        e.BrokenDescription = string.Format(
                            "{0} 必须是 {1}。".Translate(),
                            e.DisplayProperty(),
                            this.RegexLabel.Translate()
                            );
                    }
                }
            }
        }
예제 #7
0
        protected override void Validate(Entity entity, RuleArgs e)
        {
            var property = e.Property;

            bool isNull = false;

            if (property is IRefProperty)
            {
                var id = entity.GetRefNullableId((property as IRefProperty).RefIdProperty);
                isNull = id == null;
            }
            else
            {
                var value = entity.GetProperty(property);
                if (property.PropertyType == typeof(string))
                {
                    isNull = string.IsNullOrEmpty(value as string);
                }
                else
                {
                    isNull = value == null;
                }
            }

            if (isNull)
            {
                if (this.MessageBuilder != null)
                {
                    e.BrokenDescription = this.MessageBuilder(entity);
                }
                else
                {
                    e.BrokenDescription = string.Format("{0} 里没有输入值。".Translate(), e.DisplayProperty());
                }
            }
        }
예제 #8
0
파일: Validator.cs 프로젝트: 569550384/Rafy
        private static void CheckRules(
            Entity target,
            RulesContainer rules, BrokenRulesCollection brokenRulesList,
            ValidatorActions actions, Func<IRule, bool> ruleFilter
            )
        {
            var list = rules.GetList(true);

            var ignoreDataSourceValidations = HasAction(actions, ValidatorActions.IgnoreDataSourceValidations);
            var stopOnFirstBroken = HasAction(actions, ValidatorActions.StopOnFirstBroken);

            for (int index = 0; index < list.Count; index++)
            {
                IRule rule = list[index];

                //连接数据源的验证规则可能需要被过滤掉。
                if (ignoreDataSourceValidations && rule.ValidationRule.ConnectToDataSource) { continue; }

                //如果与指定的范围不符合,也需要过滤掉。
                if (ruleFilter != null && !ruleFilter(rule)) { continue; }

                var args = new RuleArgs(rule);

                try
                {
                    args.BrokenDescription = null;
                    rule.ValidationRule.Validate(target, args);
                }
                catch (Exception ex)
                {
                    throw new ValidationException("Properties.Resources.ValidationRulesException" + args.Property.Name + rule.Key, ex);
                }

                if (args.IsBroken)
                {
                    brokenRulesList.Add(rule, args);

                    if (stopOnFirstBroken) { break; }
                }
            }
        }
예제 #9
0
 /// <summary>
 /// 获取传入的要验证的属性列表
 /// </summary>
 /// <param name="e"></param>
 private void InitProperties(RuleArgs e)
 {
     if (this.Properties.Count == 0)
     {
         var property = e.Property as IProperty;
         if (property == null)
         {
             throw new InvalidProgramException("使用 PropertyNotExists 验证方法需要传入名称为 Properties 的参数。");
         }
         this.Properties.Add(property);
     }
 }
예제 #10
0
 protected override void Validate(Entity entity, RuleArgs e)
 {
     this.Handler(entity, e);
 }
예제 #11
0
 /// <summary>
 /// 子类重写此方法实现验证规则逻辑。
 /// 当验证出错时,需要设置 e.BrokenDescription。
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="e"></param>
 protected abstract void Validate(Entity entity, RuleArgs e);
예제 #12
0
 void IValidationRule.Validate(ManagedPropertyObject entity, RuleArgs e)
 {
     this.Validate(entity as Entity, e);
 }
예제 #13
0
 internal BrokenRule(IRule rule, RuleArgs args)
 {
     this.Rule = rule;
     this.Property = rule.Property;
     this.Description = args.BrokenDescription;
 }