表单验证
... 2021-7-29 About 1 min
# 表单验证
# 基础用法
Tips
配置验证字段的rules
的数据对象
<avue-form :option="option" @submit="submit" @error="error"></avue-form>
<script>
export default {
data() {
return {
option:{
column:[
{
label:'姓名',
prop:'name',
rules: [{
required: true,
message: "请输入姓名",
trigger: "blur"
}]
}, {
label:'性别',
prop:'sex',
rules: [{
required: true,
message: "请输入性别",
trigger: "blur"
}]
}
]
},
};
},
methods: {
submit(form,done){
this.$message.success(JSON.stringify(form));
done()
},
error(err){
this.$message.success('请查看控制台');
console.log(err)
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Expand Copy Copy
# 外置验证
<el-button size="small" @click="validate">验证表单</el-button>
<avue-form ref="form" :option="option" ></avue-form>
<script>
export default {
data() {
return {
option:{
column:[
{
label:'姓名',
prop:'name',
rules: [{
required: true,
message: "请输入姓名",
trigger: "blur"
}]
}, {
label:'性别',
prop:'sex',
rules: [{
required: true,
message: "请输入性别",
trigger: "blur"
}]
}
]
},
};
},
methods: {
validate(){
//如果存在验证不通过,msg为错误信息
this.$refs.form.validate((valid, done,msg) => {
if (valid) {
done()
} else {
console.log('error submit!!');
return false;
}
})
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Expand Copy Copy
# 自定义验证
<avue-form v-model="obj" :option="option1" @submit="submit" @error="error"></avue-form>
<script>
export default {
data() {
var validatePass = (rule, value, callback)=>{
if (value === '') {
callback(new Error('请输入密码'));
} else {
callback();
}
};
var validatePass2 = (rule, value, callback)=> {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.obj.password) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
obj:{
name:'张三',
},
option1:{
column:[
{
label:'姓名',
prop:'name',
rules: [{
required: true,
message: "请输入姓名",
trigger: "blur"
}]
}, {
label:'性别',
prop:'sex',
rules: [{
required: true,
message: "请输入性别",
trigger: "blur"
}]
},
{
label:'密码',
prop:'password',
type:'password',
rules: [{ validator: validatePass, trigger: 'blur' }]
}, {
label:'确认密码',
prop:'oldpassword',
type:'password',
rules: [{ validator: validatePass2, trigger: 'blur' }]
}
]
},
}
},
methods: {
submit(form,done){
this.$message.success(JSON.stringify(form));
done()
},
error(err){
this.$message.success('请查看控制台');
console.log(err)
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Expand Copy Copy