【问题标题】:Veevalidate: Validate form before POST request?Veevalidate:在 POST 请求之前验证表单?
【发布时间】:2019-09-30 13:33:54
【问题描述】:

我正在使用 VeevalidateBuefy 在 POST 请求之前验证表单。我能够正确验证表单,但 POST 请求方法在验证之前运行。

我对如何按顺序调用方法有点困惑:

  1. 验证表单
  2. POST 请求

代码沙盒:https://codesandbox.io/s/mj1vy2vq0j

代码概览

<b-modal>
  <form @submit.prevent="validateBeforeSubmit">
    ...
    ...
    <button type="submit" class="button is-primary" @click="postItem()">Submit</button>
    <button type="button" class="button" @click="isAddModalActive=false">Cancel</button>
  </form>
</b-modal>

<script>

...

methods: {
  validateBeforeSubmit() {
    this.$validator.validateAll().then(result => {
      if (result) {
        this.$toast.open({
          message: "Form is valid!",
          type: "is-success",
          position: "is-bottom"
        });
      }
      this.$toast.open({
        message: "Form is not valid! Please check the fields.",
        type: "is-danger",
        position: "is-bottom"
      });
    });
  },
  postItem() {
    alert("postItem function was called");
  }
}
</script>

【问题讨论】:

    标签: vue.js vee-validate buefy


    【解决方案1】:

    您可以在 validateBeforeSubmite() 函数中调用提交函数

    methods: {
      validateBeforeSubmit() {
        this.$validator.validateAll().then(result => {
          if (result) {
            this.$toast.open({
              message: "Form is valid!",
              type: "is-success",
              position: "is-bottom"
            });
           return this.postItem()// call your post function here and remove from @click in your submit button
    
          }
          this.$toast.open({
            message: "Form is not valid! Please check the fields.",
            type: "is-danger",
            position: "is-bottom"
          });
        });
      },
      postItem() {
        alert("postItem function was called");
      }
    }
    

    【讨论】:

    • 我听从了您的建议,但我面临以下问题: 1. toast 显示麻烦消息,即“表格有效”和“表格无效..” 2. 我也有一个 Edit 表单,所以我还需要为此创建另一个 validateBeforeSubmit 方法吗?
    • 尝试在 this.postItem() 后面加上 return
    猜你喜欢
    • 2023-04-02
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多