public override ValidationResult IsConditionalValid(string property, object value, ValidationContext validationContext) { var dependsOnProp = validationContext.ObjectType.GetProperty(property.Trim()); var postcode = dependsOnProp.GetValue(validationContext.ObjectInstance)?.ToString(); if (!string.IsNullOrEmpty(UKPostCodeRegex) && !string.IsNullOrEmpty(EnglishOrBFPOPostCodeRegex) && !string.IsNullOrEmpty(BfpoPostCodeRegex) && !string.IsNullOrEmpty(postcode?.Trim())) { if (IsNotBfpo) { // It applies to Town if (!ServiceFunctions.IsValidRegexValue(postcode, BfpoPostCodeRegex) && string.IsNullOrEmpty(value?.ToString())) { return(new ValidationResult(ErrorMessage)); } } // It applies to AddressLine2 & AddressLine3 (when other dependent property is valid UK BFPO postcode) else if (!IsNonEnglishBfpo && ServiceFunctions.IsValidDoubleRegexValue(postcode, UKPostCodeRegex, BfpoPostCodeRegex, true) && string.IsNullOrEmpty(value?.ToString())) { return(new ValidationResult(ErrorMessage)); } // It applies to AlternativePostCode // Here we are evaluating the value of the HomePostCode (postcode), not AlternativePostCode if (IsNonEnglishBfpo && !ServiceFunctions.IsValidDoubleRegexValue(postcode, UKPostCodeRegex, EnglishOrBFPOPostCodeRegex, true) && string.IsNullOrEmpty(value?.ToString())) { return(new ValidationResult(ErrorMessage)); } } return(null); }