コード例 #1
0
        /// <summary>
        /// Validates the specified entity with respect to the current validation attribute.
        /// </summary>
        /// <param name="entity">The entity to validate.</param>
        /// <param name="validationContext">The context information about the validation operation.</param>
        /// <returns>
        /// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult" /> class.
        /// </returns>
        protected override ValidationResult IsValid(object entity, ValidationContext validationContext)
        {
            var displayName = validationContext.DisplayName;
            var compositeResults = new CompositeValidationResult(string.Format("Validation for {0} failed!", displayName));

            if (entity is IEnumerable)
            {
                IEnumerable items = (IEnumerable)entity;

                var index = 0;
                foreach (var item in items)
                {
                    var validator = new DataAnnotationValidator();

                    validator.TryValidate(item);
                    var results = validator.ValidationResults;

                    if (results.Count != 0)
                    {
                        results.ForEach(x => compositeResults.AddResult(x, displayName, index));
                    }

                    index++;
                }

                return compositeResults;
            }
            else
            {
                var validator = new DataAnnotationValidator();

                validator.TryValidate(entity);
                var results = validator.ValidationResults;

                if (results.Count != 0)
                {
                    results.ForEach(x => compositeResults.AddResult(x, displayName));
                    return compositeResults;
                }
            }

            return ValidationResult.Success;
        }
コード例 #2
0
        public void 驗證GrandFather()
        {
            var grandFather = new GrandFather
            {
                Name = "hi",
                Fathers = new List<Father>
                {
                    new Father
                    {
                        Name= "Father1",
                        Age=81,
                        Sons = new List<Son>
                        {
                            new Son{Name="91", Age=300},
                            new Son{Name = "92", Age= 11},
                            new Son{Name = "93", Age= 202},
                        }
                    },
                    new Father
                    {
                        Age=82,
                        Sons = new List<Son>
                        {
                            new Son{Name="F2_1", Age=-1},
                            new Son{Name = "F2_2", Age= -2},
                            new Son{Name = "F2_3", Age= 4},
                        }
                    },
                }
            };

            var validator = new DataAnnotationValidator();

            var isValid = validator.TryValidate(grandFather);

            Assert.AreEqual(false, isValid);

            Assert.AreEqual(7, validator.ValidationResults.Count);
        }
コード例 #3
0
        public void 驗證Sons_IEnumerable()
        {
            var father = new Father
            {
                Age = 90,
                Name = "Fat",
                Sons = Enumerable.Repeat<Son>(new Son { Name = "91", Age = 300 }, 2)
            };

            var validator = new DataAnnotationValidator();

            var isValid = validator.TryValidate(father);

            Assert.AreEqual(false, isValid);

            Assert.AreEqual(2, validator.ValidationResults.Count);
        }
コード例 #4
0
        public void 驗證ProductModel()
        {
            var productModel = new ProductModel
            {
                MyPerson = new Person { Id = 1, Name = string.Empty, Birthday = new DateTime(1991, 9, 1) },
                MyProduct = new Product { Cost = 100, SellPrice = 105 }
            };

            var validator = new DataAnnotationValidator();

            var isValid = validator.TryValidate(productModel);

            Assert.AreEqual(false, isValid);

            Assert.AreEqual(2, validator.ValidationResults.Count);
        }
コード例 #5
0
        public void 驗證List集合是否能正常驗證Validation()
        {
            var father = new Father
            {
                Name = "Joey",
                Age = 999,
                Sons = new List<Son>
                {
                    new Son{Age=0},
                    new Son{Name=string.Empty, Age=200},
                    new Son{Name="91", Age=300},
                }
            };

            var validator = new DataAnnotationValidator();

            var isValid = validator.TryValidate(father);

            Assert.AreEqual(false, isValid);

            Assert.AreEqual(6, validator.ValidationResults.Count);
        }
コード例 #6
0
 public static void BeforeFeature()
 {
     target = new DataAnnotationValidator();
 }