Model is not being fully validated

2

Good morning, I'm testing an application to add a client, but whenever I try to add the user, test the ViewModel to check if the information is consistent, but the program does not validate the whole ViewModel ignoring the end of it, Why is this occurring?

AddClientViewModels:

public class TelefoneVM
{
    public String Telephone { get; set; }
}

public class EmailVM
{
    public String Email { get; set; }
}

public class AddClientViewModels : IValidatableObject
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [Display(Name = "Tipo Pessoa")]
    public int TypePerson { get; set; }

    [StringLength(100, ErrorMessage = "O {0} deve ter pelo menos {2} e no máximo {1} caracteres.", MinimumLength = 3)]
    [Display(Name = "Nome")]
    public String Name { get; set; }

    [StringLength(100, ErrorMessage = "A {0} deve ter pelo menos {2} e no máximo {1} caracteres.", MinimumLength = 3)]
    [Display(Name = "Razão Social")]
    public String CompanyName { get; set; }

    [RegularExpression(@"^\([1-9]{2}\) [2-9][0-9]{3,4}\-[0-9]{4}$", ErrorMessage = "Telefone está em um formato inválido.")]
    [Display(Name = "Telefone")]
    public List<TelefoneVM> Telephones { get; set; }

    [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "E-mail está em um formato inválido.")]
    [Display(Name = "E-mail")]
    public List<EmailVM> Emails { get; set; }


    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (TypePerson == 1 && String.IsNullOrEmpty(Name)) 
            yield return new ValidationResult("Nome é obrigatório", new[] { nameof(Name) });

        if (TypePerson == 2 && String.IsNullOrEmpty(CompanyName))
            yield return new ValidationResult("Razão Social é obrigatório", new[] { nameof(CompanyName) });
    }
}

This last part, it simply ignores:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (TypePerson == 1 && String.IsNullOrEmpty(Name)) 
            yield return new ValidationResult("Nome é obrigatório", new[] { nameof(Name) });

        if (TypePerson == 2 && String.IsNullOrEmpty(CompanyName))
            yield return new ValidationResult("Razão Social é obrigatório", new[] { nameof(CompanyName) });
    }

In this way I can not validate if the user is physical or legal, and I can not return the required field error, thank you very much if you can help me.

    
asked by anonymous 19.09.2018 / 14:57

0 answers