Пример #1
0
        public void ValidateEmptyOptional()
        {
            var validationAttr = new InnerAttribute(typeof(RegularExpressionAttribute), @"^\d{11}$");
            var optional       = new Maybe <string>();

            Assert.IsTrue(validationAttr.IsValid(optional));
        }
Пример #2
0
        public void ValidateNonEmptyOptional()
        {
            var validationAttr = new InnerAttribute(typeof(RegularExpressionAttribute), @"\d{11}$");
            var optionalOk     = new Maybe <string>("13901234567");
            var optionalBad    = new Maybe <string>("12345");

            Assert.IsTrue(validationAttr.IsValid(optionalOk));
            Assert.IsFalse(validationAttr.IsValid(optionalBad));
        }
Пример #3
0
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                // get a reference to the property this validation depends upon
                var containerType = validationContext.ObjectInstance.GetType();
                var field         = containerType.GetProperty(this.DependentProperty);

                if (field != null)
                {
                    // get the value of the dependent property
                    var dependentvalue = field.GetValue(validationContext.ObjectInstance, null);

                    // compare the value against the target value
                    if ((dependentvalue == null && this.TargetValue == null) || (dependentvalue != null && dependentvalue.Equals(this.TargetValue)))
                    {
                        // match => means we should try validating this field
                        if (!InnerAttribute.IsValid(value))
                        {
                            // validation failed - return an error
                            return(new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName }));
                        }
                    }
                }
                return(ValidationResult.Success);
            }