有没有一种简单的方法来触发表单验证 onSubmit 而不是使用 bootstrap 和 Vue.Js 的 onLoad?

Is there a simple way to trigger form validation onSubmit instead of onLoad with bootstrap and Vue.Js?

我有一个密码重置表单,当然最初是空的,我想在提交时验证 none 个字段是空的。我正在为每个字段执行以下操作:

    <b-form-group id="currentPassword" 
              label="Old password"
              :invalid-feedback="oldPasswordFeedback">
        <b-form-input id="password" 
                      v-model="passwords.currentPassword" 
                      placeholder="Enter your old Password" 
                      type="password"  
                      maxlength="60" 
                      required/>
    </b-form-group>

表单定义如下

  <b-form validated="" id="passwordChangeForm" @submit.prevent="changePassword" class="container-fluid">

问题是,每当我导航到该页面时,默认情况下所有字段都标记为无效并显示错误消息,因为它们是空的。我希望在提交表单时进行验证。有没有一种简单的方法可以做到这一点而无需安装额外的验证插件?

在您的组件声明中,您设置了 required 属性,它会在您加载页面时尝试验证您的表单,因此您可以利用 Vue.js 并创建 req 属性 在您的数据对象中,最初设置为 false

我提供了以下简化示例来向您展示如何做到这一点

new Vue({
  el: '#app',

  data() {
    return {
    item:'',
     currentPassword:'',
     req:false
    }
  },
  methods: {
  changePassword: function() {
    this.req=true;
    }
  }

});
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />

<!-- Add this after vue.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>


<div id="app" class="container">
  <div>
    
    <b-form validated="" id="passwordChangeForm" @submit.prevent="changePassword" class="container-fluid">
      <b-form-group id="currentPassword" label="Old password">
        <b-form-input id="password" v-model="currentPassword" placeholder="Enter your old Password" type="password" :required="req" />
      </b-form-group>
      <b-button type="submit" variant="primary">Submit</b-button>
    </b-form>
  </div>
</div>