Message from the validate datetimepicker

Asked

Viewed 65 times

3

Good morning, everyone,

I’m having a problem with my form validate() with the datetimepicker in a modal, the message is showing up to the right of the input and before the button, I need it to scroll down from the input, as it normally is, follow code below:

<div class="modal-body">
    <form enctype="multipart/form-data" id="cadAcesso" method="POST">
    <div class="row">
         <div class="col-md-4 col-sm-4 col-xs-12 col-lg-3">
              <label for="inputDtEntrada_" style="color:red; font-weight: bold;">*</label>
              <label for="inputDtEntrada_" class="control-label">Dt./Hora Entrada</label>     
              <div class="control-group form-group input-group date datetimepicker" id="inputDtEntrada_" data-target-input="nearest" title="Data e Hora de Entrada">
                 <input type="text" class="form-control datetimepicker-input" data-target="#inputDtEntrada_" id="inputDtEntrada" name="inputDtEntrada"/>
                 <div class="input-group-append" data-target="#inputDtEntrada_" data-toggle="datetimepicker">
                     <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                 </div>
              </div>
         </div>
    </div>
    </form>
</div>

<script id="Js" type="text/javascript">
$(document).ready(function() {
    $("#cadAcesso").validate({
    rules: {
    "inputDtEntrada": {
    required: true
    }
    },

    messages: {
    "inputDtEntrada": {
    required: 'Campo obrigatório'
    }
    }
    });
});
</script> 

Below follows print of the problem:

inserir a descrição da imagem aqui

  • this is bootstrap 3 or 4? What is the link of the plugin of this validator that you used? Only with this code do not simulate your problem

  • To using bootstrap 4 the plugin is https://tempusdominus.github.io/bootstrap-4/ about the validator is from the html form itself. Thank you.

  • guy couldn’t simulate the invalid field, but probably what happens is that bootstrap usually puts some styles in the elements by default, so this label with the error message must be getting some bootstrap css. Actually use the element label to display the error message is not very suitable to my view. I believe he does it because as has a for on the label when clicking on it the Focus goes to the corresponding input, even so do not agree, but this has nothing to do with the problem tb rss

  • Because it’s Hugo, I don’t know why by default the bootstrap or html itself inserts the label after the input and the right not below, I’m not able to solve or find anything on the web about. I saw that something like this gives to be done ( errorPlacement: Function(error, element) { error.appendTo( element.parents("div").next("div") ); },) but it throws the error all wrong in the other input =/

1 answer

1


The problem is that Validator creates a label with the error message after the input to be validated, and how you put a div with the calendar icon after input, the label created will be between input and div.

One way to resolve this is to move the created labels to after the div of the calendar icon using the option function invalidHandler Validator. This function is triggered whenever a field is identified as invalid.

In the function you take all elements with the class .datetimepicker-input and check if after them there is a label.error, and if there is, you move this label to after the next div (which is the div of the calendar icon) using .insertAfter(). Only that the function of invalidHandler is fired before the plugin creates the error labels, so you need to use a setTimeout to give a small delay before executing the code.

Put CSS so that these labels are invisible and do not take up starting space:

.datetimepicker label.error{
   position: absolute;
   visibility: hidden;
}

Take an example:

$(document).ready(function() {
    $("#cadAcesso").validate({
    rules: {
       "inputDtEntrada": {
       required: true
       },
       "inputDtSaida": {
       required: true
       }
    },

    messages: {
      "inputDtEntrada": {
         required: 'Campo obrigatório'
      },
      "inputDtSaida": {
         required: 'Campo obrigatório'
      }
    },
   invalidHandler: function() {
      setTimeout(function(){
         $.each($(".datetimepicker-input"), function(){
            var erros = $(this).next("label.error");
            if(erros.length){
               erros
               .css("flex-basis", "100%")
               .insertAfter(erros.parent())
               .css({
                  "position" : "relative",
                  "top" : "-.7rem",
                  "visibility" : "visible"
               });
            }
         });
         
      }, 1);
   }
    });
});
.datetimepicker label.error{
   position: absolute;
   visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalExemplo">
  Abrir modal
</button>
<div class="modal fade" id="modalExemplo" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
<div class="modal-body">
    <form enctype="multipart/form-data" id="cadAcesso" method="POST">
    <div class="row">
         <div class="col-md-4 col-sm-4 col-xs-12 col-lg-3">
              <label for="inputDtEntrada_" style="color:red; font-weight: bold;">*</label>
              <label for="inputDtEntrada" class="control-label">Dt./Hora Entrada</label>     
              <div class="control-group form-group input-group date datetimepicker" id="inputDtEntrada_" data-target-input="nearest" title="Data e Hora de Entrada">
                 <input type="text" class="form-control datetimepicker-input" data-target="#inputDtEntrada_" id="inputDtEntrada" name="inputDtEntrada"/>
                 <div class="input-group-append" data-target="#inputDtEntrada_" data-toggle="datetimepicker">
                     <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                 </div>
              </div>
         </div>
         <div class="col-md-4 col-sm-4 col-xs-12 col-lg-3">
              <label for="inputDtSaida_" style="color:red; font-weight: bold;">*</label>
              <label for="inputDtSaida" class="control-label">Dt./Hora Saída</label>     
              <div class="control-group form-group input-group date datetimepicker" id="inputDtSaida_" data-target-input="nearest" title="Data e Hora de Saida">
                 <input type="text" class="form-control datetimepicker-input" data-target="#inputDtSaida_" id="inputDtSaida" name="inputDtSaida"/>
                 <div class="input-group-append" data-target="#inputDtSaida_" data-toggle="datetimepicker">
                     <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                 </div>
              </div>
         </div>
    </div>
    <button>Enviar</button>
    </form>
</div>
    </div>
  </div>
</div>

  • Sam in mine he jumped to after the button, see in the image I attached my original question please. Is there the possibility to give a next next next next? I don’t know...

  • I just changed one line: if(erros.length) erros.css("flex-basis", "100%").insertAfter(erros.next("div")); See if it works now.

  • Sam worked perfectly, you’re the guy, only three points to highlight. The first is that by giving the mandatory error the field and button are no longer rounded at the tips. The second is that when you click the button to issue the error the message appears very quickly inside the input and soon after below it. The third point would like to know what it means(css("flex-Basis")) if you can inform me would you like to know please.

  • Man, it’s really weird how the corners of the button don’t get rounded anymore. I will try to solve this and the question of appearing quickly inside the input, then I explain the flex-Basis thing.

  • You really have to make a maneuver to position these Abels. I changed the answer by putting a CSS (take a look). The Labels should stay after the main div of the icon so that the edges are not affected. flex-Basis: 100% forces the element to occupy the entire width of the flexbox element, causing it to be below the others.

  • I put a "top" : "-.7rem" in the code to move the message up a bit, then you adjust how you think it looks better.

  • 1

    Good old Sam... Now ta perfect... thank you so much again, they put me on a fire here at the company to develop a system, but thank God we have people like you to help us here at Stack haha

Show 2 more comments

Browser other questions tagged

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