Boxes of selections required at least 1 with Data Annotations

2

No 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 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 post button "Create". When clicking "Create" button, show red warning when the 3 buttons checkbox is 3 false. Must be at least 1 true to process post.

How to do this using Data Annotations?

    
asked by anonymous 02.01.2017 / 20:47

1 answer

2

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

Interface IValidatableObject .

With it you can perform some more 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 new[] { "Exemplo1", "Exemplo2", "Exemplo3" }); is the location where the validation message will be displayed, that is, here: @Html.ValidationMessageFor(model => model.Exemplo1, "", new { @class = "text-danger" }) . If you want to display only Example1 you do not need to add the others.

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 perform the check in the controller and add an error in ModelState or you can even develop your own CustomAttribute.

    
02.01.2017 / 21:12