选择字段上的Bootstrap 4验证中的问题 [英] issue in Bootstrap 4 validation on select field

查看:116
本文介绍了选择字段上的Bootstrap 4验证中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery和Bootstrap的新手,我正在使用jquery和Bootstrap 4验证我的表单模式,每当出现错误时,它都必须在相应字段下方显示错误,但在我的情况下,select字段会错误覆盖,选择字段消失,但对于输入字段来说效果很好.

I'm new to jQuery and Bootstrap, I'm using jquery and Bootstrap 4 for validation of my form modal, whenever there is an error it must show the error below the corresponding fields, but in my case the select field gets overwritten by the error and select field disappears but it works fine for input field.

这里有一个外观,如果您想仔细查看图像,只需单击它即可.

here have a look and if you want to have a close look on image just click on it..

如您所见,选择字段被fieldError覆盖,但是对于输入字段来说很好.

As you can see the select fields get overwritten by the fieldError but it's fine for input field.

这是我的jQuery验证代码:

here's my jQuery validation code:

$(function(){
    setCategorySelect();

    $(document).on('shown.bs.modal','#manageItemsModal', function () {
        $('#manageItemsModal #btnSubmit').on('click', function(){
            if (validateForm()) {
                messageSuccess("Very well");
            } else {
                messageError("Oops!!");
            }
        });
    });
});

function validateForm() {
    var validationStatus = true;

    if ($('#manageItemsForm #selectedCategory').val().length == 0) {
        showFieldError(('#manageItemsForm #selectedCategory'), 'Must not be blank');
        if (validationStatus) { $('#manageItemsForm #selectedCategory').focus() };
        validationStatus = false;
    }

    if ($('#manageItemsForm #selectedBrandModel').val().length == 0) {
        showFieldError(('#manageItemsForm #selectedBrandModel'), 'Must not be blank');
        if (validationStatus) { $('#manageItemsForm #selectedBrandModel').focus() };
        validationStatus = false;       
    }

    if ($('#manageItemsForm #serialNo').val().length == 0) {
        showFieldError(('#manageItemsForm #serialNo'), 'Must not be blank');
        if (validationStatus) { $('#manageItemsForm #serialNo').focus() };
        validationStatus = false;       
    }

    if ($('#manageItemsForm #selectedVendor').val().length == 0) {
        showFieldError(('#manageItemsForm #selectedVendor'), 'Must not be blank');
        if (validationStatus) { $('#manageItemsForm #selectedVendor').focus() };
        validationStatus = false;       
    }

    if ($('#manageItemsForm #selectedBranch').val().length == 0) {
        showFieldError(('#manageItemsForm #selectedBranch'), 'Must not be blank');
        if (validationStatus) { $('#manageItemsForm #selectedBranch').focus() };
        validationStatus = false;       
    }   

    return validationStatus;    
}

function showFieldError(element, message) {
    $(element).addClass('is-invalid');
    $(element).next().html(message);
    $(element).next().show();
}

function clearFieldError(element) {
    $(element).removeClass('is-invalid');
    $(element).removeAttr('required');
    $(element).next().html('');
}

function setCategorySelect() {
    var $categorySelect = $('#manageItemsForm #selectedCategory').selectize({
        selectOnTab: true,
        closeAfterSelect: true,
        persist: false,
        create: false,
        valueField: 'id',
        labelField: 'text',
        options: [],
        preload: true,
        onInitialize : function() {
            var self = this;
            $.ajax({
                url: '/assetCategory/search',
                type: 'POST',
                dataType: 'json',
                data: {
                    searchText: '*'
                },                  
                error: function() {
                    callback();
                },
                success: function(res) {
                    self.addOption(res.data);
                }
            });
        },
        load: function(query, callback) {
            if (query.length <= 2) return callback();
            $.ajax({
                url: '/assetCategory/search',
                type: 'POST',
                dataType: 'json',
                data: {
                    searchText: query + "*"
                },
                error: function() {
                    callback();
                },
                success: function(res) {
                    console.log(res.data);
                    callback(res.data);
                    $categorySelect.refreshItems();
                },
                fail : function() {
                    callback();
                }
            });   
        }
    });         
}

这是我的HTML:

<div class="modal-body">
    <form id="manageItemsForm">
         <input type="hidden" id="id" name="id">
              <div class="row">
                  <div class="col-4">
                    <div class="form-group">
                       <label for="selectedCategory" class="col-form-label"><span class="text-danger">* </span>Category</label>
                            <select class="form-control" name="selectedCategory" id="selectedCategory"></select>    
                     <div class="invalid-feedback"></div>
                    </div>
                   </div>
                    <div class="col-8">
                        <div class="form-group">
                            <label for="selectedBrandModel" class="col-form-label"><span class="text-danger">* </span>Brand & Model</label>
                                <select class="form-control" name="selectedBrandModel" id="selectedBrandModel"></select>
                                    <div class="invalid-feedback"></div>
                         </div>                         
                    </div>
               </div>
               <div class="row">
                   <div class="col-4">
                        <div class="form-group">
                            <label for="serialNo" class="col-form-label"><span class="text-danger">* </span>Serial No.</label>
                                  <input type="text" class="form-control" id="serialNo" name="serialNo">
                                    <div class="invalid-feedback"></div>
                        </div>
                    </div>
                    <div class="col-8">
                       <div class="form-group">
                           <label for="description" class="col-form-label">Description</label>
                               <input type="text" class="form-control" id="description" name="description">
                                    <div class="invalid-feedback"></div>
                        </div>                          
                    </div>
               </div> 
               <div class="row">
                   <div class="col-6">
                       <div class="form-group">
                          <label for="selectedVendor" class="col-form-label"><span class="text-danger">* </span>Purchase Vendor</label>
                              <select class="form-control" name="selectedVendor" id="selectedVendor"></select>  
                                    <div class="invalid-feedback"></div>                                
                    </div>
               </div>
               <div class="col-3">
                   <div class="form-group">
                       <label for="selectedVendor" class="col-form-label"><span class="text-danger">* </span>Purchase Date</label>
                            <div class="input-group date" data-date-format="dd-M-yyyy">
                                 <input type="text" class="form-control" id="purchaseDate" name="purchaseDate" />
                                        <span class="input-group-text input-group-append input-group-addon"><i class="simple-icon-calendar"></i></span>
                                    </div>
                            <div class="invalid-feedback"></div>                                
                    </div>
                </div>
                <div class="col-3">
                    <div class="form-group">
                        <label for="supportTillDate" class="col-form-label"><span class="text-danger">* </span>Support till date</label>
                            <div class="input-group date" data-date-format="dd-M-yyyy">
                               <input type="text" class="form-control" id="supportTillDate" name="supportTillDate" />
                                        <span class="input-group-text input-group-append input-group-addon"><i class="simple-icon-calendar"></i></span>
                          </div>
                          <div class="invalid-feedback"></div>                              
                    </div>
               </div>                                                       
           </div>
           <div class="row">
              <div class="col-9">
                  <div class="form-group">
                     <label for="selectedBranch" class="col-form-label"><span class="text-danger">* </span>Branch</label>
                            <select class="form-control" name="selectedBranch" id="selectedBranch"></select>    
                                <div class="invalid-feedback"></div>
                    </div>  
               </div>
               <div class="col-3">
                   <label for="purchasePrice" class="col-form-label">Purchase Price</label>
                        <div class="input-group">
                            <div class="input-group-prepend"><span class="input-group-text input-group-addon" style="padding: 0.4rem 0.75rem 0.3rem 0.75rem;">&#8377;</span></div>
                            <input id="purchasePrice" name="purchasePrice" type="text" class="form-control" aria-label="Amount" style="text-align:right;">
                                </div>
                                <div class="invalid-feedback"></div>
                            </div>                          
                        </div>  
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button id="btnSubmit" type="button" class="btn btn-primary">Save</button>
                </div>
            </div>

通过我在Spring Boot中使用jQuery的方式,除了通过jQuery进行验证之外,其他所有东西都可以正常工作(保存,更新,删除).

By the way I am using jQuery in Spring boot and everything is working fine(save, update, delete) except for validation from jQuery.

请帮助!

推荐答案

问题出在HTML中,.input-group的节点始终没有相同的结构.在某些情况下,例如HTML之类的输入之后,您就有.invalid-feedback

The problem is in Your HTML, the nodes of your .input-group does not have allways the same structure. In some cases you have .invalid-feedback just after the input such as this HTML

<div class="form-group">
  <label for="serialNo" class="col-form-label"><span class="text-danger">* 
  </span>Serial No.</label>
  <input type="text" class="form-control" id="serialNo" name="serialNo">
  <div class="invalid-feedback"></div>
</div>

对于其他字段,.invalid-feedback不在输入之后,而是在.form-group之外.看看

For other fields the .invalid-feedback isn't after the input but outside from .form-group. take a look

<div class="input-group date" data-date-format="dd-M-yyyy">
   <input type="text" class="form-control" id="purchaseDate" name="purchaseDate" />
   <span class="input-group-text input-group-append input-group-addon">
       <i class="simple-icon-calendar"></i>
   </span>
 </div>
 <div class="invalid-feedback"></div>

表单的HTML结构上的这种差异使您的showFieldError()和clearFieldError()始终无法按预期工作,因为$(element).next()不能捕获用于插入/删除验证消息的正确DOM节点.因此,在某些情况下,clearFieldError会删除错误的HTML标记,这会使您的选择消失

This difference in HTML structure of the form made your showFieldError() and clearFieldError() not working allways as you expected, because $(element).next() don't catch the right DOM node for insert/remove the validation message. So in some cases clearFieldError remove the wrong HTML tag and this can make your selects disappear

function showFieldError(element, message) {
    $(element).addClass('is-invalid');
    $(element).next().html(message);
    $(element).next().show();
}

function clearFieldError(element) {
    $(element).removeClass('is-invalid');
    $(element).removeAttr('required');
    $(element).next().html('');
}

因此,您必须修复HTML才能为所有字段获取相同的结构.始终将<div class="invalid-feedback"></div>放在选择或输入字段的正下方.否则,您必须根据HTML更改传递给showFieldError()和clearFieldError()函数的选择器

So you have to fix Your HTML to obtain the same structure for all fields. Put the <div class="invalid-feedback"></div> allways just below the select or input field. Otherwise you have to change the selector that you pass to showFieldError() and clearFieldError() functions according to your HTML

否则,一种简单的方法是将一个ID添加到具有.invalid-feedback类的div中,该ID可以通过他的相关输入ID轻松地进行管理,例如

Otherwise a simply approach is to add a ID to divs with class .invalid-feedback, an ID which you can easily manage by his related input ID, something like

<div class="input-group date" data-date-format="dd-M-yyyy">
       <input type="text" class="form-control" id="purchaseDate" name="purchaseDate" />
       <span class="input-group-text input-group-append input-group-addon">
           <i class="simple-icon-calendar"></i>
       </span>
     </div>
     <div id="purchaseDate_err_mex" class="invalid-feedback"></div>

通过这种方式,您可以将输入名称传递给函数,它们将变为

in this way you can pass the input name to your functions and them becomes

    function showFieldError(input_id, message) {
        $('#'+input_id).addClass('is-invalid');
        $('#'+ input_id +'_err_mex').html(message).show();
    }

    function clearFieldError(input_id) {
        $('#'+input_id).removeClass('is-invalid');
        //$('#'+input_id).removeAttr('required'); 
       /* don't need to remove required attribute from mandatory fields */
        $('#'+ input_name +'_err_mex').html('').hide();
    }

和验证功能

function validateForm() {
    var validationStatus = true;

    if ($('#selectedCategory').val().length == 0) {
        showFieldError('selectedCategory', 'Must not be blank');
        if (validationStatus) { $('#selectedCategory').focus() };
        validationStatus = false;
    }

 ........

    return validationStatus;    
}

您仅检查所有字段的长度是否大于0,以便可以在循环中验证整个表单

You only check if the length of all fields is more than 0, so you can validate the entire form within a loop

function validateForm() {
        var validationStatus = true;
        var form_inputs = $('#manageItemsForm input, #manageItemsForm select')


        $.each(form_inputs,function(){

            var input_id = $(this).attr('name');
            clearFieldError(input_id);
            if ($.trim($(this).val()).length == 0 && $(this).is("[required]")) {
                showFieldError(input_id, 'Must not be blank');
                if (validationStatus) { $('#'+input_id).focus() };
                validationStatus = false;
            }
        });

        return validationStatus;    
    }

这篇关于选择字段上的Bootstrap 4验证中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆