2

所以我有一个有两个提交按钮的表单,但是对于每个我想验证不同的字段,在这种情况下,当我单击预览提交按钮时,我想忽略 3 个字段,而在“正常”提交按钮中,我想全部验证。这可能吗?

这是一些html:

<form class="form" role="form" id="searchesForm">
  
   <input class="form-control" type="text" id="name" name="name"/>
   <input class="form-control" type="text" id="widgetWidth"  name="widgetWidth" /> 
   <input class="form-control" type="text" id="widgetHeight"  name="widgetHeight" />
   <!-- More fields validated for both button clicks-->
   ...
  
   <button type="button" id="preview" name="previewSearch" class="btn btn-default" >
     Preview
   </button>
   <button type="submit" id="submit" name="submitSave" class="btn btn-primary">
     Save
   </button>
</form>

所以想法是,当我按下预览按钮时,这 3 个字段(名称、widgetWidth 和 widgetHeight)应该被忽略,但当我按下保存按钮时则不会。

这是我的引导验证器:

$('#searchesForm').bootstrapValidator({
                excluded: ['td>select,td>input,td>textarea'],
                feedbackIcons:{
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                live:'submitted',
                submitButtons:'[type="submit"]',
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                enabled:function(){
                                    debugger;
                                    alert("testes");
                                },
                                message: 'The question required and cannot be empty'
                            }
                        }
                    }
                }
            }).on('success.form.bv',function(e){
                // Prevent form submission
                e.preventDefault();
                //check which button was pressed
                var button=$(e.target).data('bootstrapValidator').getSubmitButton();

                if($(button).attr("id")==="preview"){
                    
                    previewSearch();//do ajax stuff

                }else{
                    saveSearch();//do ajax stuff
                }

             
            }).on('error.form.bv',function(e){

                var invalidFields=$(e.target).data('bootstrapValidator').getInvalidFields();

                //show tab for specific fields location
                $.each(invalidFields,function(index,value){

                    if($("#filtros").has(value).length > 0){
                        $('#tabs li:eq(0) a').tab('show')
                    }
                    if($("#resultado").has(value).length > 0){
                        $('#tabs li:eq(1) a').tab('show')
                    }
                    if($("#widget").has(value).length > 0){
                        $('#tabs li:eq(2) a').tab('show')
                    }
                });
            }).on('status.field.bv', function (e, data) {

                if (data.bv.getSubmitButton()) {
                    data.bv.disableSubmitButtons(false);
                }

                if(data.bv.getSubmitButton()) {
                    if (data.bv.getSubmitButton().attr("id") === "preview"){
                        if (data.field === "name" || data.field === "widgetWidth" || data.field === "widgetHeight") {
                            
                            data.bv.$form.bootstrapValidator('enableFieldValidators', data.field, 'notEmpty', false);
                        }
                    }else{
                       
                        data.bv.$form.bootstrapValidator('enableFieldValidators', data.field, 'notEmpty', true);
                    }
                }
            });

我真的不知道我们在哪里可以根据按下的按钮启用/禁用字段验证器。我试图在 .on('status.field.bv'...) 方法上更改字段验证器的状态,但它不能完全工作。如果我选择“type=submit”以外的东西,submitButtons 似乎也不起作用。

那么有没有人提出过这种情况?

4

0 回答 0