在VUEJS中的错误时免费提交表格

发布于 2025-01-31 21:27:56 字数 3869 浏览 0 评论 0原文

我目前正在Vue中构建表格,并且当表格没有错误消息时,我很难提交表格。

我目前已解决,以便错误显示何时应显示,但目标是在按下提交按钮后更改页面。

我将感谢任何可以帮助我的技巧:)

html

<template>
  <form @submit.prevent="submitMessage">
    <div class="form-control">
      <label for="user-name">Name*</label>
      <input id="user-name" name="user-name" type="text" v-model="userName" />
    </div>
    <div class="form-control">
      <label for="age">Age</label>
      <input id="age" name="age" type="number" v-model="userAge" />
    </div>
    <div class="form-control">
      <label for="email">Email*</label>
      <input id="email" name="email" type="email" v-model="email" />
    </div>
    <div class="form-control">
      <label for="referrer">How did you hear about us?</label>
      <select id="referrer" name="referrer" v-model="referrer">
        <option value="google">Google</option>
        <option value="wom">Word of mouth</option>
        <option value="newspaper">Social Media</option>
        <option value="other">Other</option>
      </select>
    </div>
    <div class="form-control">
      <label for="message">Message*</label>
      <textarea rows="5" cols="50" id="message" name="message" v-model="message">Aa</textarea>
    </div>
    <div>
      <button id="send" @click="sendForm()">Send Message</button>
    </div>
    <div class="errors">
      <p v-if="errors.length > 0">
        <b>Please correct the following error(s):</b>
        <ul>
          <li v-for="error in errors">{{ error }}</li>
        </ul>
      </p>
    </div>
  </form>
</template>

vue

<script>

export default {
  data() {
    return {
      userName: '',
      userAge: null,
      referrer: 'google',
      email: '',
      message: '',
      errors: []
    };
  },
  methods: {
    submitMessage(e) {
      this.userName = '';
      this.userAge = null;
      this.email = '';
      this.referrer = 'google';
      this.message = ''
    },
    sendForm() {
      this.errors = [];
      if (!this.userName) {
        this.errors.push('Name is required');
      }
      if (!this.email) {
        this.errors.push('Email is required');
      } else if (!this.validEmail(this.email)) {
        this.errors.push('Valid email required.');
      }
      if (!this.message) {
        this.errors.push('Message is required');
      }
      if (!this.errors.length) {
        return true;
      }
      if (this.errors.any()) {
        this.$router.push('/thankyou');
      }
    },
    validEmail: function (email) {
      var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      return re.test(email);
    }
  }
}

</script>

router.js

import {
    createRouter,
    createWebHistory
} from 'vue-router';


import HomeBeerSearch from './pages/beers/HomeBeerSearch.vue';
import BeerList from './pages/beers/BeerList.vue';
import CustomerSupport from './pages/contact/CustomerSupport.vue';
import ThankYou from './pages/contact/ThankYou.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [{
            path: '/',
            component: HomeBeerSearch
        },
        {
            path: '/beers',
            component: BeerList
        },
        {
            path: '/support',
            component: CustomerSupport
        },
        {
            path: '/thankyou',
            component: ThankYou
        }
    ]
});

export default router

I'm currently building a form in Vue and I'm having a hard time submitting the form when the form is free from error messages.

I'm currently fixed so that the errors show when they should but the goal is to change the page after pressing the submit button.

I would appreciate any tip that could help me :)

HTML

<template>
  <form @submit.prevent="submitMessage">
    <div class="form-control">
      <label for="user-name">Name*</label>
      <input id="user-name" name="user-name" type="text" v-model="userName" />
    </div>
    <div class="form-control">
      <label for="age">Age</label>
      <input id="age" name="age" type="number" v-model="userAge" />
    </div>
    <div class="form-control">
      <label for="email">Email*</label>
      <input id="email" name="email" type="email" v-model="email" />
    </div>
    <div class="form-control">
      <label for="referrer">How did you hear about us?</label>
      <select id="referrer" name="referrer" v-model="referrer">
        <option value="google">Google</option>
        <option value="wom">Word of mouth</option>
        <option value="newspaper">Social Media</option>
        <option value="other">Other</option>
      </select>
    </div>
    <div class="form-control">
      <label for="message">Message*</label>
      <textarea rows="5" cols="50" id="message" name="message" v-model="message">Aa</textarea>
    </div>
    <div>
      <button id="send" @click="sendForm()">Send Message</button>
    </div>
    <div class="errors">
      <p v-if="errors.length > 0">
        <b>Please correct the following error(s):</b>
        <ul>
          <li v-for="error in errors">{{ error }}</li>
        </ul>
      </p>
    </div>
  </form>
</template>

Vue

<script>

export default {
  data() {
    return {
      userName: '',
      userAge: null,
      referrer: 'google',
      email: '',
      message: '',
      errors: []
    };
  },
  methods: {
    submitMessage(e) {
      this.userName = '';
      this.userAge = null;
      this.email = '';
      this.referrer = 'google';
      this.message = ''
    },
    sendForm() {
      this.errors = [];
      if (!this.userName) {
        this.errors.push('Name is required');
      }
      if (!this.email) {
        this.errors.push('Email is required');
      } else if (!this.validEmail(this.email)) {
        this.errors.push('Valid email required.');
      }
      if (!this.message) {
        this.errors.push('Message is required');
      }
      if (!this.errors.length) {
        return true;
      }
      if (this.errors.any()) {
        this.$router.push('/thankyou');
      }
    },
    validEmail: function (email) {
      var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      return re.test(email);
    }
  }
}

</script>

Router.js

import {
    createRouter,
    createWebHistory
} from 'vue-router';


import HomeBeerSearch from './pages/beers/HomeBeerSearch.vue';
import BeerList from './pages/beers/BeerList.vue';
import CustomerSupport from './pages/contact/CustomerSupport.vue';
import ThankYou from './pages/contact/ThankYou.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [{
            path: '/',
            component: HomeBeerSearch
        },
        {
            path: '/beers',
            component: BeerList
        },
        {
            path: '/support',
            component: CustomerSupport
        },
        {
            path: '/thankyou',
            component: ThankYou
        }
    ]
});

export default router

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2025-02-07 21:27:56

您可以在SPA上通过VUE路由器更改页面:

this.$router.push({ name: 'route name' })

或者可以将RAW JavaScript用于外部链接:

window.location.href = 'http://www.google.com';

You can change the page by Vue router on SPA:

this.$router.push({ name: 'route name' })

or you can use raw JavaScript for external links:

window.location.href = 'http://www.google.com';
烧了回忆取暖 2025-02-07 21:27:56
    // there is no such function errors.any()
    if (this.errors.any()) {
        this.$router.push('/thankyou');
    }

您正确地进行导航,但我认为IF Statment逻辑有些缺陷。而是尝试一下:

    // check if there are errors; if not go to the thank you page
    if (!this.errors.length) {
         this.$router.push('/thankyou');
    }
    // there is no such function errors.any()
    if (this.errors.any()) {
        this.$router.push('/thankyou');
    }

Your doing the navigation correctly but I think that the if statment logic is a little flawed. Try this instead:

    // check if there are errors; if not go to the thank you page
    if (!this.errors.length) {
         this.$router.push('/thankyou');
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文