Validation control Errormessage

Asked

Viewed 161 times

1

I got the following model:

public class Request : IEntity
{
    public int Id { get; set; }

    [Required(ErrorMessage = "O Centro de Responsabilidade é obrigatório!")]
    public string Cadastro_Id { get; set; }
    public virtual Cadastro Cadastro { get; set; }

    [Required(ErrorMessage = "A requisição é obrigatória!")]
    public string Nome { get; set; }

    public bool DN { get; set; }

    public string Matricula { get; set; }

    public string Sessao { get; set; }

    object IEntity.Id
    {
        get { return Id; }
        set { Id = (int)value; }
    }
}

All fields automatically loaded when entering the page are required. In this form there is a checkbox (DN) that if marked, makes it mandatory to complete the Registration and Session. If DN is not marked, these two fields do not appear and are not mandatory (use in script to hide the fields).

Follow my view:

<table>       
     <tr>
         <td>@Html.DropDownList("Cadastro_Id", string.Empty)</td>
         <td>@Html.DropDownList("Nome", string.Empty)</td>
     </tr>
</table>

<br />
<div id="dn">
    @Html.CheckBoxFor(x => x.DN) <b> NACIONAL </b>
</div> 

<div><br /></div>

<div class="ocultar">
  <b> Número de Matrícula </b> 
</div>
<div class="ocultar">
    @Html.EditorFor(x => x.Matricula)
</div>
    <div class="ocultar">
  <b> Sessão </b> 
</div>
<div class="ocultar">
    @Html.TextAreaFor(x => x.Sessao)
</div>

How to do so in case the checkbox is marked the fields are mandatory and when it is not marked the fields remain hidden and without validation?

1 answer

2


Implementing the interface IValidatableObject:

public class Request : IEntity, IValidatableObject
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "O Centro de Responsabilidade é obrigatório!")]
    public string Cadastro_Id { get; set; }
    public virtual Cadastro Cadastro { get; set; }

    [Required(ErrorMessage = "A requisição é obrigatória!")]
    public string Nome { get; set; }

    public bool DN { get; set; }

    public string Matricula { get; set; }

    public string Sessao { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (DN) {
            if (String.IsNullOrEmpty(Matricula)) {
                yield return new ValidationResult("A Matrícula é obrigatória!", new List<string> { "Matricula" } ); 
            }

            if (String.IsNullOrEmpty(Sessao)) {
                yield return new ValidationResult("A Sessão é obrigatória!", new List<string> { "Sessao" } ); 
            }
        }
    }
}
  • Sorry @Ciganomorrisonmendez, I did not understand very well. The fields that only need to be mandatory if DN is marked is session and enrollment. I will edit the question with the class of my entire model because I believe I cannot change it to Ivalidatableobject

  • Why can’t you?

  • 1

    Oh yes... it worked @Gypsy. Perfect!

Browser other questions tagged

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