Validation for Textarea using Razor MVC 4

Asked

Viewed 678 times

1

I have the following modal and need to include validations so that the textarea is mandatory when clicking the "Yes" button, that is, it cannot be sent in white, and so that the textarea has at least 15 characters.

  @model TClient
  <div id="myModal" class="modal fade">
      <div class="modal-dialog">
          <div class="modal-content">
              @using (Html.BeginForm())
              {
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                      <h4 class="modal-title">@(Model.IsLocked ? "Desbloquear" : "Bloquear") cliente</h4>
                  </div>
                  <div class="modal-body">
                      <p>Deseja realmente @(Model.IsLocked ? "desbloquear" : "bloquear") o cliente <strong>@Model.Name</strong>?</p>
                      <div class="row">
                          <div class="col-lg-12">
                              <hr />
                              <div class="form-group">
                                  <label class="control-label">
                                      Motivo:
                                  </label>
                                  <div>
                                      @this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)
                                  </div>
                              </div>
                          </div>
                      </div>
                  </div>
                  <div class="modal-footer">
                      <button class="btn" data-dismiss="modal" aria-hidden="true">
                          Não
                      </button>
                      <button type="submit" id="confirmar" class="btn btn-primary">
                          Sim
                      </button>
                  </div>
              }
          </div>
      </div>
  </div>
  • You are using jQuery.Validate?

  • @Ciganomorrisonmendez I believe so, because there are several validations in the application that I work in javascript, jquery, etc. What would be your proposal for the questions? 'Cause I haven’t found anything about it so far

  • 1

    I don’t understand what it is here: @this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50). You can edit your question with the source code of these methods?

  • @Ciganomorrisonmendez is where I create the Textarea with Razor, in MVC 4

1 answer

1

I can’t quite see this approach:

@model TClient

...

@this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)

The correct would be to strongly type the View:

@model MeuSistema.Models.MeuModel

And use the following cliché:

@Html.TextAreaFor(model => model.Motivo, new { @class = "form-control required", @cols = 50, @rows = 7 })
@Html.ValidationMessageFor(model => model.Motivo)

If you memorized the Model correctly:

[Required]
[DataType(DataType.MultilineText)]
public String Motivo { get; set; }

And jQuery.Validate is installed correctly, should work without further settings. Still, you can force the form validation as follows (jQuery):

$('#botaoSubmit').on('click', function() {
    $("#form").valid();
});

Browser other questions tagged

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