Selections boxes required at least 1 with Data Annotations

Asked

Viewed 50 times

2

In the model I have:

[Display(Name = "Exemplo 1")]
[Range(typeof(bool), "true", "true", ErrorMessage = "Erro, marcar como true")]
public bool Exemplo1 { get; set; }

[Display(Name = "Exemplo 2")]
public bool Exemplo2 { get; set; }

[Display(Name = "Exemplo 3")]
public bool Exemplo3 { get; set; }

In the view I have :

@Html.CheckBoxFor(model => model.Exemplo1, new { id = "toggle1", data_on = "Sim", data_off = "Não", data_toggle = "toggle", data_class = "fast", data_size = "mini", data_onstyle = "success", data_offstyle = "danger" })
@Html.ValidationMessageFor(model => model.Exemplo1, "", new { @class = "text-danger" })

@Html.CheckBoxFor(model => model.Exemplo2, new { id = "toggle2", data_on = "Sim", data_off = "Não", data_toggle = "toggle", data_class = "fast", data_size = "mini", data_onstyle = "success", data_offstyle = "danger" })

@Html.CheckBoxFor(model => model.Exemplo3, new { id = "toggle3", data_on = "Sim", data_off = "Não", data_toggle = "toggle", data_class = "fast", data_size = "mini", data_onstyle = "success", data_offstyle = "danger" })

I have a "Create" post button. By clicking "Create" button, show red warning when the 3 checkbox buttons is 3 false. Must be at least 1 true to process post.

How to do this using Data Annotations ?

1 answer

2


What you want is a conditional validation. There are several ways to do this and some of them I will demonstrate below:

Interface Ivalidatableobject.

With it you can perform some extra checks on the Model. For your example, it would look something like this:

using System.ComponentModel.DataAnnotations;

public class MeuModel : IValidatableObject
{

    [Display(Name = "Exemplo 1")]
    [Range(typeof(bool), "true", "true", ErrorMessage = "Erro, marcar como true")]
    public bool Exemplo1 { get; set; }

    [Display(Name = "Exemplo 2")]
    public bool Exemplo2 { get; set; }

    [Display(Name = "Exemplo 3")]
    public bool Exemplo3 { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Exemplo1 == false && Exemplo2 == false && Exemplo3 == false)
        {
            yield return new ValidationResult("Erro, marcar como true",
                         new[] { "Exemplo1", "Exemplo2", "Exemplo3" });
        }
    }
}

Where is new[] { "Exemplo1", "Exemplo2", "Exemplo3" }); will be the place where the validation message will be displayed, i.e., here: @Html.ValidationMessageFor(model => model.Exemplo1, "", new { @class = "text-danger" }). If you wish to display only the Example1 no need to add the other.

More details can be seen in this answer.

Expressive Annotations

This is a package with several custom attributes to suit various activities. Its use would look something like this:

[Display(Name = "Exemplo 1")]
[RequiredIf("Exemplo1 == false && Exemplo2 == false && Exemplo3 == false",
ErrorMessage = "Erro, marcar como true")]
public bool Exemplo1 { get; set; }

[Display(Name = "Exemplo 2")]
public bool Exemplo2 { get; set; }

[Display(Name = "Exemplo 3")]
public bool Exemplo3 { get; set; }

More details in this answer.

Another option would be to check the controller and add an error in the ModelState or you yourself develop your own Customattribute.

  • Ivalidatableobject Interface option worked. It works like [Required] instead of using [Range(typeof(bool), "true", "true", Errormessage = "Error, mark as true")] ?

  • 1

    @Matheusmiranda With the interface you can add the validation you want. Check if it is null, if it is greater than such a date, if it needs another value and what your mind desires. Remembering that there is no need to use this if the Data Annotations meet.

Browser other questions tagged

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