0

I have two models :

public class CardPayment
{
   [Required]
    public Card CardDetail { get; set; }

    [Required]
    [MaxLength(100)]
    public string Description { get; set; }
}
public class Card
{
    [Required]
    [MaxLength(50)]
    public string CardHolder { get; set; }

    [Required, MaxLength(3)]
    public string Cv2 { get; set; }
}

I am validating the models with the following bit of code

var context = new ValidationContext(cardPayment);
var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();

if (Validator.TryValidateObject(cardPayment, context, results, true))
{
}

If i pass in a model with a description that has 101+ characters the validation works and the results collection has the validation error.

If i pass in the same model but set the Cv2 field to be 4+ characters it does not get picked up.

Is it possible for the TryValidateObject to validate the inner model?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.