【Vue】ivew表单验证失败
<template>
<div class="form abs-center">
<Form ref="formValue" v-model="formValue" :rules="formRules" :label-width="80"><FormItem label="Name" prop="name">
<Input v-model="formValue.name" placeholder="Enter your name"></Input>
</FormItem>
<FormItem label="Country" prop="country">
<Select v-model="formValue.country" placeholder="Select your country">
<Option value="China">China</Option>
<Option value="U.S.A.">U.S.A.</Option>
<Option value="Janpan">Janpan</Option>
</Select>
</FormItem>
</Form>
<script>
export default {
name: 'Step1',
data () {
return {formValue: {
name: '',
country: ''
},
formRules: {
name: [
{ required: true, message: 'The name cannot be empty', trigger: 'blur' }
],
country: [
{ required: true, message: 'Please select the country', pattern: '/.+/', trigger: 'change' }
]
}
}
}
}
</script>
开始验证时,第一个name不管填不填都会触发错误信息,第二个country选择了值会触发错误信息,不太明白问题出在哪里
回答
你好,我把你代码贴出来运行了,改了两个地方就好了。
第一个:
<Form ref="formValue" :model="formValue" :rules="formRules" :label-width="80">
v-model="formValue"
->:model="formValue"
第二个{ required: true, message: 'Please select the country', pattern: '/.+/', trigger: 'change' }
->{ required: true, message: 'Please select the country', trigger: 'change' }
以下完整代码
<template><div class="form abs-center">
<Form ref="formValue" :model="formValue" :rules="formRules" :label-width="80">
<FormItem label="Name" prop="name">
<Input v-model="formValue.name" placeholder="Enter your name" />
</FormItem>
<FormItem label="Country" prop="country">
<Select v-model="formValue.country" placeholder="Select your country" prop="country" @on-change="change">
<Option value="China">China</Option>
<Option value="U.S.A.">U.S.A.</Option>
<Option value="Janpan">Janpan</Option>
</Select>
</FormItem>
</Form>
</div>
</template>
<script>
export default {
name: "Step1",
data() {
return {
formValue: {
name: "",
country: ""
},
formRules: {
name: [
{
required: true,
message: "The name cannot be empty",
trigger: "blur"
}
],
country: [
{
required: true,
message: "Please select the country",
// pattern: "/.+/",
trigger: "change"
}
]
}
};
},
};
</script>
以上是 【Vue】ivew表单验证失败 的全部内容, 来源链接: utcz.com/a/85907.html