Model not being fully validated

Asked

Viewed 74 times

2

Good morning Guys, I’m testing an application to add a client, but whenever I try to add the user, test the Viewmodel to see if the information is consistent, but the program does not validate all Viewmodel ignoring the end of it, would know why this is happening ?

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) });
    }
}

That last part, he just 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) });
    }

This way I can not validate if the user is physical or legal, and I can not return the mandatory field error, I really appreciate if you can help me.

  • It will only fire if all the other validations have passed, I think it can even force the Validate(), but I don’t see much point in it.

  • The problem is that it is not being fired, even if all validations have occurred, because let’s say when I send the field Nome empty, it does not return that the field is required

  • What do you mean by shoot? You’re looking at the ModelState in Controller?

  • That, I send the blank form, and it’s not returning me the error that O nome é obrigatário, he’s ignoring that mistake.

  • Here it is working... include your action in the question, you are returned to with the model? the post is ajax?

  • I discovered the problem, now I’m trying to solve, as I use dynamic phone and email fields in the form, I’m passing a model with a list by the controller when rendering the screen model.Telephones.Add(new TelefoneVM() { Telephone = "" }); and model.Emails.Add(new EmailVM() { Email = "" }); this is interfering, and not returning the mistakes.

  • So I think we can close this question and open a new case find another difficulty

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.