引导验证,标志着整个造型以红色,即使某些字段是正确的(Bootstrap validator mar

2019-10-24 03:40发布

我试图建立一个与BootstrapValidator对此我以非常有效的新的长期形式。

我最大的问题是,如果一个字段是不正确它标志着ALL与即使成功复选标记留在红色着色字段。 形态正常提交,但可视确认是什么,似乎很关闭。
在行动中看到的形式....你可以真正看到问题,如果您单击什么也没有填写提交,然后进行正确填写进去。 http://chelseaporter.com/APSoPC/adoptForm3.php这里是我的HTML代码和我的JavaScript代码段。 谢谢大家!

<div class="form-group">
      <label class="col-sm-3 control-label" for="name">Name</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" id="name" name="name" placeholder="Your Full Name" />
      </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="email">Email Address</label>
        <div class="col-sm-7">
          <input type="text" class="form-control" id="email" name="email" placeholder="Your email address" />
        </div>
      </div>
    <div class="form-group">
            <label class="col-sm-3 control-label" for="address">Street Address</label>
        <div class="col-sm-7">
        <input type="text" class="form-control" id="address" name="address" placeholder="Your Street Address">
    </div>
    </div>

 <!--many more fields)-->

    <div class="form-group">
      <div class="col-md-6 col-md-offset-2">
        <button type="submit" class="btn btn-success">Submit</button>
      </div>
    </div>
    </form>
  </div><!-- closes panel Body--> 

$(document).ready(function () {
var validator = $("#adoption-form").bootstrapValidator({
    //live: 'disabled',
    feedbackIcons: {
        valid: "glyphicon glyphicon-ok",
        invalid: "glyphicon glyphicon-remove", 
        validating: "glyphicon glyphicon-refresh"
    }, 
    fields : {
        name :{
            validators : {
                notEmpty : {
                    message : "Please provide your name."
                }, 
                stringLength: {
                    min : 4, 
                    max: 35,
                    message: "Name must be between 4 and 35 characters long"
                },
            }//end validators
        },  
        email :{
            validators : {
                notEmpty : {
                    message : "Please provide an email address"
                },
                regexp: {
                        regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
                        message: 'The value is not a valid email address'
                }
            }//end validators
        },  
        address : {
            validators : {
                notEmpty : {
                    message: "Address is required"
                }
            }//end validators
        },
        city : {
            validators : {
                notEmpty : {
                    message: "City is required"
                }
            }//end validators
        },
        state : {
            validators : {
                notEmpty : {
                    message: "State is required"
                }
            }//end validators
        },
        zip : {
            validators : {
                notEmpty : {
                    message: "Zip is required"
                }
            }//end validators
        },
        years : {
            validators : {
                notEmpty : {
                    message: "Years at current address is required"
                },
                numeric : {
                    message: "Valid number of years lived at current address is required (only numbers)"
                }
            }//end validators
        },
        hPhone : {
            validators : {
                notEmpty : {
                    message: "Home phone is required. If you only have one phone please enter that number for Home Phone AND Alt Phone."
                },
                phone : {
                    country: 'US',
                    message: "Valid phone number is required (only numbers)"
                }
            }//end validators
        },
        altPhone : {
            validators : {
                phone : {
                    country: 'US',
                    message: "Valid phone number is required"
                }
            }//end validators
        },
        dNumber : {
            validators : {
                notEmpty : {
                    message: "Driver's License number is required"
                },
                numeric : {
                    message: "Valid Driver's License number is required (only numbers)"
                }
            }//end validators
        },
        dState : {
            validators : {
                notEmpty : {
                    message: "Driver's License state is required"
                }
            }//end validators
        },
        ePhone : {
            validators : {
                phone : {
                    country: 'US',
                    message: "Valid phone number is required"
                }
            }//end validators
        },
        hType : {
            validators : {
                notEmpty : {
                    message: "Home Type required."
                }
            }//end validators
        },
        hStatus : {
            validators : {
                notEmpty : {
                    message: "Home Status Required"
                }
            }//end validators
        },
        lNumber : {
            validators : {
                phone : {
                    country: 'US',
                    message: "Valid phone number is required"
                }
            }//end validators
        },
        student : {
            validators : {
                notEmpty : {
                    message: "Please answer if you are a student."
                }
            }//end validators
        },


    } //end ALL validators  
});

    validator.on("success.form.bv", function (e) {
         if (data.fv.getInvalidFields().length > 0) {    // There is invalid field
            data.fv.disableSubmitButtons(true);
        }
        e.preventDefault();
        $("#adoption-form").addClass("hidden");
        $("#confirmation").removeClass("hidden");

         var $form = $(e.target),
                            fv    = $form.data('bootstrapValidator');           
});

//process the form
$("#adoption-form").submit(function(event) {

    // get the form data
    // there are many ways to get this data using jQuery (you can use the class or id also)
    var formData = {
        'name'              : $('input[name=name]').val(),
        'email'             : $('input[name=email]').val(),
        'address'           : $('input[name=address]').val(),
                    'city'                  : $('input[name=city]').val(),
                    'state'                 : $('select[name=state]').val(),
                    'zip'                   : $('input[name=zip]').val(),
                    'years'             : $('input[name=years]').val(),
                    'hPhone'            : $('input[name=hPhone]').val(),
                    'altPhone'          : $('input[name=altPhone]').val(),
                    'dNumber'           : $('input[name=dNumber]').val(),
                    'dState'            : $('input[name=dState]').val(),
                    'employer'          : $('input[name=employer]').val(),
                    'ePhone'            : $('input[name=ePhone]').val(),
                    'hType'             : $('select[name=hType]').val(),
                    'hStatus'           : $('select[name=hStatus]').val(),
                    'lName'             : $('input[name=lName]').val(),
                    'LNumber'           : $('input[name=LNumber]').val(),
                    'student'           : $('select[name=student]').val(),
                    'sName'             : $('input[name=sName]').val()
    };

    // process the form
    $.ajax({
        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url         : 'process.php', // the url where we want to POST
        data        : formData, // our data object
        dataType    : 'json', // what type of data do we expect back from the server
        encode          : true
    })
        // using the done promise callback
        .done(function(data) {

            // log data to the console so we can see
            console.log(data); 
                            console.log(formData);

                });
    // stop the form from submitting the normal way and refreshing the page
    event.preventDefault();
            console.log(formData);
}); 

});

Answer 1:

它看起来像验证工作正常。 您的表单元素对他们“有-成功”级,但被过度写的“有错误”的类。 这是令人困惑,因为没有父元素的有“有错误”的类。 它看起来像它的正确添加仅是完整的元素“有-成功”级。 把一个重要的你的“有不成功”类的颜色,所以它看起来是这样的:

.has-success{
      color: #3c763d !important; }

您将覆盖此BS的代码,但我觉得很奇怪它不是摆在首位正确造型吧。 此外,它看起来像你的“备用电话”字段是表示没有任何东西是完整的。 虽然,这可能是技术上是正确的,因为它可能不是一个必填字段,可以在“完整的”,在这种情况下具有比这差UX其他的“完整的”不同的含义。

让我知道这很重要的工作。



文章来源: Bootstrap validator marks entire form in red even when some fields are correct