DataAnnotationsValidatorRecursive icon indicating copy to clipboard operation
DataAnnotationsValidatorRecursive copied to clipboard

for list of complex object its not validating.

Open shaikhazhar opened this issue 7 years ago • 1 comments

I have used your Nuget package to use the your DataAnnotationsValidatorRecursive. Below is the simplified version of my code. I am not getting validation error for List<MyAddress> although i am getting validation errors for ID. Please help.

public class Program { static void Main(string[] args) { var child = new Child(); var validationResults = new List<ValidationResult>();
child.clientAddress = new List<MyAddress > { new MyAddress { Address1 = "", Address2 = "", Address3 = "", ZipCode = 0, State = "", City = "" } };

        child.BaseValidator = new DataAnnotationsValidator.DataAnnotationsValidator();
        child.BaseValidator.TryValidateObjectRecursive(child, validationResults);
        if (validationResults.Count > 0)
        {
            child.LogValidationResult(validationResults);
        }

} }

public class Child: Base { [Required] public int ID{ get; set; };

    [Required]
    public List<MyAddress> myAddress;

}

public class MyAddress { [Required] [StringLength(40)] public string Address1 { get; set; }

    public string Address2 { get; set; }

    [Required]
    [StringLength(40)]
    public string Address3 { get; set; }

    [Required]
    [StringLength(9, MinimumLength = 5, ErrorMessage = "ZipCode minimum length is 5 and maximum length is 9.")]
    public int ZipCode;

    [Required]
    [StringLength(2)]
    public string State { get; set; }

    [Required]
    [StringLength(40, MinimumLength = 1, ErrorMessage = "city minimum length is 1 and maximum length is 40.")]
    public   string City{ get; set; }
}

public abstract class Base : IValidatableObject { public IDataAnnotationsValidator BaseValidator;

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        //TODO:loop the validation results

        throw new NotImplementedException();
    }


    public void LogValidationResult(List<ValidationResult> resultList)
    {
        //TODO: Log validation result.
    }
}

shaikhazhar avatar Dec 29 '17 13:12 shaikhazhar

Are you saying that it short-circuits before it validates the list (because it fails validation for the ID)? Or that it will fail to validate the list in all cases?

If you remove the ID, do you get the other validation errors you expect?

reustmd avatar Jan 16 '18 02:01 reustmd