[英]How to validate form in Vue?
I want to build register form in Vue.我想在 Vue 中构建注册表单。
I think I did everything okay by the book.我认为我按照书本做的一切都很好。 But my question is how to trigger the validation if I using v-form
from Vuetify
and vue-property-decorator
.但我的问题是,如果我使用Vuetify
和vue-property-decorator
v-form
,如何触发验证。
Because all the examples they have this.$refs.form.validate()
... For this form it's not working.因为他们所有的例子都有this.$refs.form.validate()
... 对于这种形式,它不起作用。
So, how to trigger the validation when I submit the form?那么,当我提交表单时如何触发验证呢?
This is my code:这是我的代码:
<template>
<v-container fluid fill-height>
<v-layout align-center justify-center>
<v-flex xs12 sm8 md6>
<v-container>
<v-card>
<v-toolbar dark color="primary">
<v-toolbar-title>Register</v-toolbar-title>
</v-toolbar>
<v-card-text>
<v-form v-model="loginValid">
<v-text-field v-model="form.name.value" prepend-icon="person" name="Name" label="Name" required></v-text-field>
<v-text-field v-model="form.email.value" :rules="form.email.rule" label="Email" required type="email" prepend-icon="person"></v-text-field>
<v-text-field prepend-icon="lock" v-model="form.password.value" :rules="form.password.rule" label="Password" type="password" required></v-text-field>
</v-form>
</v-card-text>
<v-card-actions class="pa-3">
<v-spacer></v-spacer>
<v-btn ref="btn-entrar" id="btn-entrar" color="primary" @click="submit">Register</v-btn>
<router-link to="/login" class="btn btn-link">Login</router-link>
</v-card-actions>
</v-card>
</v-container>
</v-flex>
</v-layout>
</v-container>
</template>
<script lang="ts">
import { Component, Watch, Prop } from 'vue-property-decorator';
import BaseComponent from '@/modules/common/components/baseComponent.vue';
import { State, Action, Getter } from 'vuex-class';
@Component({})
export default class RegisterPage extends BaseComponent {
public loginValid: boolean = false;
public form = {
name: { value: '' },
email: {
value: '',
rule: [
(v: string) => !!v || 'Email is required',
(v: string) => /.+@.+/.test(v) || 'E-mail must be valid'
]
},
password: {
value: '',
rule: [
(v: string) => !!v || 'Password is required',
(v: string) => v.length >= 8 || ''
]
}
};
public name: string = '';
public email: string = '';
public password: string = '';
constructor() {
super();
}
public submit() {
// i don't know if this form is valid or not :(
console.log('in submit');
}
}
</script>
I have not used Typescript, but ultimately, you'll have to validate each model onSubmit.我没有使用 Typescript,但最终,您必须在提交时验证每个模型。 So, in the submit()
function that you have, do preventDefault()
, validate your fields and if all okay then go ahead actually do submit the form to the backend.因此,在您拥有的submit()
函数中,执行preventDefault()
,验证您的字段,如果一切正常,则继续实际将表单提交到后端。
Do read the guide for general workflow: https://v2.vuejs.org/v2/cookbook/form-validation.html请阅读一般工作流程指南: https ://v2.vuejs.org/v2/cookbook/form-validation.html
Also, do check out Vuelidate and VeeValidate which are simple validation frameworks for VueJS.另外,请查看Vuelidate和VeeValidate ,它们是 VueJS 的简单验证框架。
ps: see: using Vuelidate with Typescript issue first for good pointers. ps:请参阅:首先将Vuelidate 与 Typescript问题一起使用以获得良好的指示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.