Conditional Client-Side (Jquery) validation

Asked

Viewed 265 times

2

Late!

I am performing a complex validation on a specific class with the following code (reduced to simplify):

public class Classe1 : IValidatableObject 
{
    [Key]
    public int Id_Classe1 { get; set; }
    [Required]
    public int Id_Estado { get; set; }
    public virtual Classe2 CLASSE2 { get; set; }

    public Classe1() {
    CLASSE2 = new Classe2();
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
    if (this.Id_Estado == 1) 
    {
        if (CLASSE2.Nm_Classe2 == null)
            yield return new ValidationResult("Campo Nome é obrigatório.", new string[] { "CLASSE2.Nm_Classe2" });
    }
}

In the controller (Modelstate.Isvalid) the validation is ok. Now I need to reflect this validation in Client-Side, with jquery unobtrusive. Can someone give me some guidance on how to do?

  • Already took a look at the plugin Validator ?

  • Yes. I use it. I happen to want the following: Case Id_Estado (attribute of classe1) be 1, then force the typing of Nm_Classe2 (attribute of classe2 that is not [Required]). Exactly what you are doing in the code above. It would be like putting a [Required] conditional on attribute CLASSE2, but since this attribute is an object of another class it simply does not validate in client-side.

2 answers

1

To whom it may interest, I found what I needed, I resolved it as follows:

I created a conditional validator that tests a class specific attribute and handles the other class attribute, like this:

public class Classe1 : IValidatableObject
{
    [Key]
    public int Id_Classe1 { get; set; }
    [Required]
    public int Id_Estado { get; set; }
    [Required]
    public virtual Classe2 CLASSE2 { get; set; }

    [IgualOuDiferente("Id_Estado", "1", "Igual", ErrorMessage = "Campo requerido")]
    public string Teste
    {
        get { return this.CLASSE2.Nm_Classe2; }
        set { CLASSE2.Nm_Classe2 = value; }
    }
}

That is, if the attribute "Id_status" is "Equal" to "1", requires you to enter the Nm_Classe2, even though he’s in another class (Classe2) without the [Required].

In View it looks like this:

@Html.TextBoxFor(m => m.Teste)
@Html.ValidationMessageFor(m => m.Teste)

Valew!

0

Implementing custom attributes that implement an interface called IClientValidatable. In this question users show in detail how to do this.

In this case, your event Validate enters as a validation bonus, only on the server side.

  • I read the question, including I already implement a customized method that tests a specific field that, if it contains a certain value, requires filling in another field. It is exactly what I want, it happens that I came across the problem that the field that I am requiring is in another class, and I can not find how to do this validation. If I put one [Required] just above the attribute CLASSE2, it does not work because it is an object that contains other attributes.

  • Place the validation attribute in the field that should be validated from your CLASSE2. The ModelState is able to validate a property of an aggregated class even if the class in question is the class containing its aggregated class.

  • I appreciate the suggestion, the problem is that there is screen that uses this same class and, on this screen, it cannot be required. Thus, I really wanted to make an attribute of another class required according to the value defined in an attribute in the current class (exactly as described above).

Browser other questions tagged

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