Exemplo n.º 1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if ((value is DateTime))
            {
                if ((DateTime)value != DateTime.MinValue)
                {
                    return(ValidationResult.Success);
                }
            }
            else if ((value != null) && (!string.IsNullOrWhiteSpace(value.ToString())))
            {
                return(ValidationResult.Success);
            }
            var model = validationContext.ObjectInstance;
            var otherPropertyValue = GetProperty.Value(model, otherProperty);

            if (otherPropertyValue == null)
            {
                return(ValidationResult.Success);
            }

            if (otherPropertyValue.ToString() == otherPropertyCheckTriggerValue)
            {
                return(new ValidationResult($"The {GetDisplayNameAttribute.Value(model, validationContext.MemberName)} field is required."));
            }
            return(ValidationResult.Success);
        }
Exemplo n.º 2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var model = validationContext.ObjectInstance;
            var otherPropertyValue = GetProperty.Value(model, otherProperty);

            if (otherPropertyValue.Equals(value))
            {
                return(ValidationResult.Success);
            }
            var memberDisplayName        = GetDisplayNameAttribute.Value(model, validationContext.MemberName);
            var otherPropertyDisplayName = GetDisplayNameAttribute.Value(model, otherProperty);

            return(new ValidationResult($"{memberDisplayName} must match {otherPropertyDisplayName}."));
        }
Exemplo n.º 3
0
 protected override ValidationResult IsValid(object value, ValidationContext validationContext)
 {
     if (!IsDefaultValue.Value(value))
     {
         return(ValidationResult.Success);
     }
     else
     {
         var model = validationContext.ObjectInstance;
         if (!IsDefaultValue.Value(GetProperty.Value(model, otherProperty)))
         {
             var memberDisplayName        = GetDisplayNameAttribute.Value(model, validationContext.MemberName);
             var otherPropertyDisplayName = GetDisplayNameAttribute.Value(model, otherProperty);
             return(new ValidationResult($"{memberDisplayName} must have value, when {otherPropertyDisplayName} has value."));
         }
         return(ValidationResult.Success);
     }
 }
Exemplo n.º 4
0
        public void GetValue(string name, object expected)
        {
            //Arrange
            var testModel = new TestModel();

            testModel.IntOne      = 1;
            testModel.IntTwo      = 2;
            testModel.StringA     = "A";
            testModel.StringABC   = "ABC";
            testModel.StringEmtpy = "";
            testModel.BoolTrue    = true;
            testModel.BoolFalse   = false;

            //Act
            var value = GetProperty.Value(testModel, name);

            //Assert
            Assert.AreEqual(expected, value);
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var model = validationContext.ObjectInstance;
            var otherPropertyValue = GetProperty.Value(model, otherProperty);

            if (otherPropertyValue == null)
            {
                return(ValidationResult.Success);
            }

            if (value != null)
            {
                return(ValidationResult.Success);
            }

            if (!string.IsNullOrEmpty(otherPropertyValue.ToString().Trim()))
            {
                var memberDisplayName        = GetDisplayNameAttribute.Value(model, validationContext.MemberName);
                var otherPropertyDisplayName = GetDisplayNameAttribute.Value(model, otherProperty);
                return(new ValidationResult($"{memberDisplayName} must have value, when {otherPropertyDisplayName} has value"));
            }

            return(ValidationResult.Success);
        }