2020-08-13 15:18:26 +08:00
## Form 表单
由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据
2021-06-26 14:48:18 +08:00
:::tip
组件升级采用了 flex 布局,以替代旧版本的 float 布局。
:::
2020-08-13 15:18:26 +08:00
### 典型表单
包括各种表单项,比如输入框、选择器、开关、单选框、多选框等。
:::demo 在 Form 组件中,每一个表单域由一个 Form-Item 组件构成,表单域中可以放置各种类型的表单控件,包括 Input、Select、Checkbox、Radio、Switch、DatePicker、TimePicker
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
< el-form ref = "form" :model = "form" label-width = "80px" >
< el-form-item label = "活动名称" >
< el-input v-model = "form.name" > < / el-input >
< / el-form-item >
< el-form-item label = "活动区域" >
< el-select v-model = "form.region" placeholder = "请选择活动区域" >
< el-option label = "区域一" value = "shanghai" > < / el-option >
< el-option label = "区域二" value = "beijing" > < / el-option >
< / el-select >
< / el-form-item >
< el-form-item label = "活动时间" >
< el-col :span = "11" >
2021-09-03 14:13:58 +08:00
< el-date-picker
type="date"
placeholder="选择日期"
v-model="form.date1"
style="width: 100%;"
>< / el-date-picker >
2020-08-13 15:18:26 +08:00
< / el-col >
< el-col class = "line" :span = "2" > -< / el-col >
< el-col :span = "11" >
2021-09-03 14:13:58 +08:00
< el-time-picker
placeholder="选择时间"
v-model="form.date2"
style="width: 100%;"
>< / el-time-picker >
2020-08-13 15:18:26 +08:00
< / el-col >
< / el-form-item >
< el-form-item label = "即时配送" >
< el-switch v-model = "form.delivery" > < / el-switch >
< / el-form-item >
< el-form-item label = "活动性质" >
< el-checkbox-group v-model = "form.type" >
< el-checkbox label = "美食/餐厅线上活动" name = "type" > < / el-checkbox >
< el-checkbox label = "地推活动" name = "type" > < / el-checkbox >
< el-checkbox label = "线下主题活动" name = "type" > < / el-checkbox >
< el-checkbox label = "单纯品牌曝光" name = "type" > < / el-checkbox >
< / el-checkbox-group >
< / el-form-item >
< el-form-item label = "特殊资源" >
< el-radio-group v-model = "form.resource" >
< el-radio label = "线上品牌商赞助" > < / el-radio >
< el-radio label = "线下场地免费" > < / el-radio >
< / el-radio-group >
< / el-form-item >
< el-form-item label = "活动形式" >
< el-input type = "textarea" v-model = "form.desc" > < / el-input >
< / el-form-item >
< el-form-item >
< el-button type = "primary" @click =" onSubmit " > 立即创建</ el-button >
< el-button > 取消< / el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
2021-09-03 14:13:58 +08:00
desc: '',
},
2020-08-13 15:18:26 +08:00
}
},
methods: {
onSubmit() {
2021-09-03 14:13:58 +08:00
console.log('submit!')
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
:::tip
W3C 标准中有如下[规定](https://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2):
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
> <i>When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.</i>
2021-06-19 02:33:00 +08:00
即:当一个 form 元素中只有一个输入框时,在该输入框中按下回车应提交该表单。如果希望阻止这一默认行为,可以在 `<el-form>` 标签上添加 `@submit.prevent` 。
2020-08-13 15:18:26 +08:00
:::
### 行内表单
当垂直方向空间受限且表单较简单时,可以在一行内放置表单。
:::demo 设置 `inline` 属性可以让表单域变为行内的表单域
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
< el-form :inline = "true" :model = "formInline" class = "demo-form-inline" >
< el-form-item label = "审批人" >
< el-input v-model = "formInline.user" placeholder = "审批人" > < / el-input >
< / el-form-item >
< el-form-item label = "活动区域" >
< el-select v-model = "formInline.region" placeholder = "活动区域" >
< el-option label = "区域一" value = "shanghai" > < / el-option >
< el-option label = "区域二" value = "beijing" > < / el-option >
< / el-select >
< / el-form-item >
< el-form-item >
< el-button type = "primary" @click =" onSubmit " > 查询</ el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
formInline: {
user: '',
2021-09-03 14:13:58 +08:00
region: '',
},
2020-08-13 15:18:26 +08:00
}
},
methods: {
onSubmit() {
2021-09-03 14:13:58 +08:00
console.log('submit!')
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
### 对齐方式
根据具体目标和制约因素,选择最佳的标签对齐方式。
:::demo 通过设置 `label-position` 属性可以改变表单域标签的位置,可选值为 `top` 、`left`,当设为 `top` 时标签会置于表单域的顶部
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
< el-radio-group v-model = "labelPosition" size = "small" >
< el-radio-button label = "left" > 左对齐< / el-radio-button >
< el-radio-button label = "right" > 右对齐< / el-radio-button >
< el-radio-button label = "top" > 顶部对齐< / el-radio-button >
< / el-radio-group >
< div style = "margin: 20px;" > < / div >
2021-09-03 14:13:58 +08:00
< el-form
:label-position="labelPosition"
label-width="80px"
:model="formLabelAlign"
>
2020-08-13 15:18:26 +08:00
< el-form-item label = "名称" >
< el-input v-model = "formLabelAlign.name" > < / el-input >
< / el-form-item >
< el-form-item label = "活动区域" >
< el-input v-model = "formLabelAlign.region" > < / el-input >
< / el-form-item >
< el-form-item label = "活动形式" >
< el-input v-model = "formLabelAlign.type" > < / el-input >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
labelPosition: 'right',
formLabelAlign: {
name: '',
region: '',
2021-09-03 14:13:58 +08:00
type: '',
},
}
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
### 表单验证
在防止用户犯错的前提下,尽可能让用户更早地发现并纠正错误。
:::demo Form 组件提供了表单验证的功能,只需要通过 `rules` 属性传入约定的验证规则,并将 Form-Item 的 `prop` 属性设置为需校验的字段名即可。校验规则参见 [async-validator ](https://github.com/yiminghe/async-validator )
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
2021-09-03 14:13:58 +08:00
< el-form
:model="ruleForm"
:rules="rules"
ref="ruleForm"
label-width="100px"
class="demo-ruleForm"
>
2020-08-13 15:18:26 +08:00
< el-form-item label = "活动名称" prop = "name" >
< el-input v-model = "ruleForm.name" > < / el-input >
< / el-form-item >
< el-form-item label = "活动区域" prop = "region" >
< el-select v-model = "ruleForm.region" placeholder = "请选择活动区域" >
< el-option label = "区域一" value = "shanghai" > < / el-option >
< el-option label = "区域二" value = "beijing" > < / el-option >
< / el-select >
< / el-form-item >
< el-form-item label = "活动时间" required >
< el-col :span = "11" >
< el-form-item prop = "date1" >
2021-09-03 14:13:58 +08:00
< el-date-picker
type="date"
placeholder="选择日期"
v-model="ruleForm.date1"
style="width: 100%;"
>< / el-date-picker >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< / el-col >
< el-col class = "line" :span = "2" > -< / el-col >
< el-col :span = "11" >
< el-form-item prop = "date2" >
2021-09-03 14:13:58 +08:00
< el-time-picker
placeholder="选择时间"
v-model="ruleForm.date2"
style="width: 100%;"
>< / el-time-picker >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< / el-col >
< / el-form-item >
< el-form-item label = "即时配送" prop = "delivery" >
< el-switch v-model = "ruleForm.delivery" > < / el-switch >
< / el-form-item >
< el-form-item label = "活动性质" prop = "type" >
< el-checkbox-group v-model = "ruleForm.type" >
< el-checkbox label = "美食/餐厅线上活动" name = "type" > < / el-checkbox >
< el-checkbox label = "地推活动" name = "type" > < / el-checkbox >
< el-checkbox label = "线下主题活动" name = "type" > < / el-checkbox >
< el-checkbox label = "单纯品牌曝光" name = "type" > < / el-checkbox >
< / el-checkbox-group >
< / el-form-item >
< el-form-item label = "特殊资源" prop = "resource" >
< el-radio-group v-model = "ruleForm.resource" >
< el-radio label = "线上品牌商赞助" > < / el-radio >
< el-radio label = "线下场地免费" > < / el-radio >
< / el-radio-group >
< / el-form-item >
< el-form-item label = "活动形式" prop = "desc" >
< el-input type = "textarea" v-model = "ruleForm.desc" > < / el-input >
< / el-form-item >
< el-form-item >
2021-09-03 14:13:58 +08:00
< el-button type = "primary" @click =" submitForm (' ruleForm ')"
>立即创建< /el-button
>
2020-08-13 15:18:26 +08:00
< el-button @click =" resetForm (' ruleForm ')" > 重置</ el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
ruleForm: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
2021-09-03 14:13:58 +08:00
desc: '',
2020-08-13 15:18:26 +08:00
},
rules: {
name: [
{ required: true, message: '请输入活动名称', trigger: 'blur' },
2021-09-03 14:13:58 +08:00
{
min: 3,
max: 5,
message: '长度在 3 到 5 个字符',
trigger: 'blur',
},
2020-08-13 15:18:26 +08:00
],
region: [
2021-09-03 14:13:58 +08:00
{ required: true, message: '请选择活动区域', trigger: 'change' },
2020-08-13 15:18:26 +08:00
],
date1: [
2021-09-03 14:13:58 +08:00
{
type: 'date',
required: true,
message: '请选择日期',
trigger: 'change',
},
2020-08-13 15:18:26 +08:00
],
date2: [
2021-09-03 14:13:58 +08:00
{
type: 'date',
required: true,
message: '请选择时间',
trigger: 'change',
},
2020-08-13 15:18:26 +08:00
],
type: [
2021-09-03 14:13:58 +08:00
{
type: 'array',
required: true,
message: '请至少选择一个活动性质',
trigger: 'change',
},
2020-08-13 15:18:26 +08:00
],
resource: [
2021-09-03 14:13:58 +08:00
{ required: true, message: '请选择活动资源', trigger: 'change' },
2020-08-13 15:18:26 +08:00
],
desc: [
2021-09-03 14:13:58 +08:00
{ required: true, message: '请填写活动形式', trigger: 'blur' },
],
},
}
2020-08-13 15:18:26 +08:00
},
methods: {
submitForm(formName) {
2021-09-04 19:29:28 +08:00
this.$refs[formName].validate((valid) => {
2020-08-13 15:18:26 +08:00
if (valid) {
2021-09-03 14:13:58 +08:00
alert('submit!')
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
console.log('error submit!!')
return false
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
})
2020-08-13 15:18:26 +08:00
},
resetForm(formName) {
2021-09-03 14:13:58 +08:00
this.$refs[formName].resetFields()
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
### 自定义校验规则
这个例子中展示了如何使用自定义验证规则来完成密码的二次验证。
:::demo 本例还使用`status-icon`属性为输入框添加了表示校验结果的反馈图标。
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
2021-09-03 14:13:58 +08:00
< el-form
:model="ruleForm"
status-icon
:rules="rules"
ref="ruleForm"
label-width="100px"
class="demo-ruleForm"
>
2020-08-13 15:18:26 +08:00
< el-form-item label = "密码" prop = "pass" >
2021-09-03 14:13:58 +08:00
< el-input
type="password"
v-model="ruleForm.pass"
autocomplete="off"
>< / el-input >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< el-form-item label = "确认密码" prop = "checkPass" >
2021-09-03 14:13:58 +08:00
< el-input
type="password"
v-model="ruleForm.checkPass"
autocomplete="off"
>< / el-input >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< el-form-item label = "年龄" prop = "age" >
< el-input v-model . number = "ruleForm.age" > < / el-input >
< / el-form-item >
< el-form-item >
< el-button type = "primary" @click =" submitForm (' ruleForm ')" > 提交</ el-button >
< el-button @click =" resetForm (' ruleForm ')" > 重置</ el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
var checkAge = (rule, value, callback) => {
if (!value) {
2021-09-03 14:13:58 +08:00
return callback(new Error('年龄不能为空'))
2020-08-13 15:18:26 +08:00
}
setTimeout(() => {
if (!Number.isInteger(value)) {
2021-09-03 14:13:58 +08:00
callback(new Error('请输入数字值'))
2020-08-13 15:18:26 +08:00
} else {
if (value < 18 ) {
2021-09-03 14:13:58 +08:00
callback(new Error('必须年满18岁'))
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
callback()
2020-08-13 15:18:26 +08:00
}
}
2021-09-03 14:13:58 +08:00
}, 1000)
}
2020-08-13 15:18:26 +08:00
var validatePass = (rule, value, callback) => {
if (value === '') {
2021-09-03 14:13:58 +08:00
callback(new Error('请输入密码'))
2020-08-13 15:18:26 +08:00
} else {
if (this.ruleForm.checkPass !== '') {
2021-09-03 14:13:58 +08:00
this.$refs.ruleForm.validateField('checkPass')
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
callback()
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
}
2020-08-13 15:18:26 +08:00
var validatePass2 = (rule, value, callback) => {
if (value === '') {
2021-09-03 14:13:58 +08:00
callback(new Error('请再次输入密码'))
2020-08-13 15:18:26 +08:00
} else if (value !== this.ruleForm.pass) {
2021-09-03 14:13:58 +08:00
callback(new Error('两次输入密码不一致!'))
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
callback()
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
}
2020-08-13 15:18:26 +08:00
return {
ruleForm: {
pass: '',
checkPass: '',
2021-09-03 14:13:58 +08:00
age: '',
2020-08-13 15:18:26 +08:00
},
rules: {
2021-09-03 14:13:58 +08:00
pass: [{ validator: validatePass, trigger: 'blur' }],
checkPass: [{ validator: validatePass2, trigger: 'blur' }],
age: [{ validator: checkAge, trigger: 'blur' }],
},
}
2020-08-13 15:18:26 +08:00
},
methods: {
submitForm(formName) {
2021-09-04 19:29:28 +08:00
this.$refs[formName].validate((valid) => {
2020-08-13 15:18:26 +08:00
if (valid) {
2021-09-03 14:13:58 +08:00
alert('submit!')
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
console.log('error submit!!')
return false
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
})
2020-08-13 15:18:26 +08:00
},
resetForm(formName) {
2021-09-03 14:13:58 +08:00
this.$refs[formName].resetFields()
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
:::tip
自定义校验 callback 必须被调用。 更多高级用法可参考 [async-validator ](https://github.com/yiminghe/async-validator )。
:::
### 动态增减表单项
:::demo 除了在 Form 组件上一次性传递所有的验证规则外还可以在单个的表单域上传递属性的验证规则
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
2021-09-03 14:13:58 +08:00
< el-form
:model="dynamicValidateForm"
ref="dynamicValidateForm"
label-width="100px"
class="demo-dynamic"
>
2020-08-13 15:18:26 +08:00
< el-form-item
prop="email"
label="邮箱"
:rules="[
{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
]"
>
< el-input v-model = "dynamicValidateForm.email" > < / el-input >
< / el-form-item >
< el-form-item
v-for="(domain, index) in dynamicValidateForm.domains"
:label="'域名' + index"
:key="domain.key"
:prop="'domains.' + index + '.value'"
:rules="{
required: true, message: '域名不能为空', trigger: 'blur'
}"
>
2021-09-03 14:13:58 +08:00
< el-input v-model = "domain.value" > < /el-input
>< el-button @click . prevent = "removeDomain(domain)" > 删除</ el-button >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< el-form-item >
2021-09-03 14:13:58 +08:00
< el-button type = "primary" @click =" submitForm (' dynamicValidateForm ')"
>提交< /el-button
>
2020-08-13 15:18:26 +08:00
< el-button @click =" addDomain " > 新增域名</ el-button >
< el-button @click =" resetForm (' dynamicValidateForm ')" > 重置</ el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
dynamicValidateForm: {
2021-09-03 14:13:58 +08:00
domains: [
{
value: '',
},
],
email: '',
},
}
2020-08-13 15:18:26 +08:00
},
methods: {
submitForm(formName) {
2021-09-04 19:29:28 +08:00
this.$refs[formName].validate((valid) => {
2020-08-13 15:18:26 +08:00
if (valid) {
2021-09-03 14:13:58 +08:00
alert('submit!')
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
console.log('error submit!!')
return false
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
})
2020-08-13 15:18:26 +08:00
},
resetForm(formName) {
2021-09-03 14:13:58 +08:00
this.$refs[formName].resetFields()
2020-08-13 15:18:26 +08:00
},
removeDomain(item) {
var index = this.dynamicValidateForm.domains.indexOf(item)
if (index !== -1) {
this.dynamicValidateForm.domains.splice(index, 1)
}
},
addDomain() {
this.dynamicValidateForm.domains.push({
value: '',
2021-09-03 14:13:58 +08:00
key: Date.now(),
})
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
### 数字类型验证
:::demo 数字类型的验证需要在 `v-model` 处加上 `.number` 的修饰符,这是 `Vue` 自身提供的用于将绑定值转化为 `number` 类型的修饰符。
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
2021-09-03 14:13:58 +08:00
< el-form
:model="numberValidateForm"
ref="numberValidateForm"
label-width="100px"
class="demo-ruleForm"
>
2020-08-13 15:18:26 +08:00
< el-form-item
label="年龄"
prop="age"
:rules="[
{ required: true, message: '年龄不能为空'},
{ type: 'number', message: '年龄必须为数字值'}
]"
>
2021-09-03 14:13:58 +08:00
< el-input
type="age"
v-model.number="numberValidateForm.age"
autocomplete="off"
>< / el-input >
2020-08-13 15:18:26 +08:00
< / el-form-item >
< el-form-item >
2021-09-03 14:13:58 +08:00
< el-button type = "primary" @click =" submitForm (' numberValidateForm ')"
>提交< /el-button
>
2020-08-13 15:18:26 +08:00
< el-button @click =" resetForm (' numberValidateForm ')" > 重置</ el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
numberValidateForm: {
2021-09-03 14:13:58 +08:00
age: '',
},
}
2020-08-13 15:18:26 +08:00
},
methods: {
submitForm(formName) {
2021-09-04 19:29:28 +08:00
this.$refs[formName].validate((valid) => {
2020-08-13 15:18:26 +08:00
if (valid) {
2021-09-03 14:13:58 +08:00
alert('submit!')
2020-08-13 15:18:26 +08:00
} else {
2021-09-03 14:13:58 +08:00
console.log('error submit!!')
return false
2020-08-13 15:18:26 +08:00
}
2021-09-03 14:13:58 +08:00
})
2020-08-13 15:18:26 +08:00
},
resetForm(formName) {
2021-09-03 14:13:58 +08:00
this.$refs[formName].resetFields()
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
:::tip
嵌套在 `el-form-item` 中的 `el-form-item` 标签宽度默认为零,不会继承 `el-form` 的 `label-width` 。如果需要可以为其单独设置 `label-width` 属性。
:::
### 表单内组件尺寸控制
通过设置 Form 上的 `size` 属性可以使该表单内所有可调节大小的组件继承该尺寸。Form-Item 也具有该属性。
:::demo 如果希望某个表单项或某个表单组件的尺寸不同于 Form 上的`size`属性,直接为这个表单项或表单组件设置自己的`size`即可。
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
```html
< el-form ref = "form" :model = "sizeForm" label-width = "80px" size = "mini" >
< el-form-item label = "活动名称" >
< el-input v-model = "sizeForm.name" > < / el-input >
< / el-form-item >
< el-form-item label = "活动区域" >
< el-select v-model = "sizeForm.region" placeholder = "请选择活动区域" >
< el-option label = "区域一" value = "shanghai" > < / el-option >
< el-option label = "区域二" value = "beijing" > < / el-option >
< / el-select >
< / el-form-item >
< el-form-item label = "活动时间" >
< el-col :span = "11" >
2021-09-03 14:13:58 +08:00
< el-date-picker
type="date"
placeholder="选择日期"
v-model="sizeForm.date1"
style="width: 100%;"
>< / el-date-picker >
2020-08-13 15:18:26 +08:00
< / el-col >
< el-col class = "line" :span = "2" > -< / el-col >
< el-col :span = "11" >
2021-09-03 14:13:58 +08:00
< el-time-picker
placeholder="选择时间"
v-model="sizeForm.date2"
style="width: 100%;"
>< / el-time-picker >
2020-08-13 15:18:26 +08:00
< / el-col >
< / el-form-item >
< el-form-item label = "活动性质" >
< el-checkbox-group v-model = "sizeForm.type" >
2021-09-03 14:13:58 +08:00
< el-checkbox-button
label="美食/餐厅线上活动"
name="type"
>< / el-checkbox-button >
2020-08-13 15:18:26 +08:00
< el-checkbox-button label = "地推活动" name = "type" > < / el-checkbox-button >
< el-checkbox-button label = "线下主题活动" name = "type" > < / el-checkbox-button >
< / el-checkbox-group >
< / el-form-item >
< el-form-item label = "特殊资源" >
< el-radio-group v-model = "sizeForm.resource" size = "medium" >
< el-radio border label = "线上品牌商赞助" > < / el-radio >
< el-radio border label = "线下场地免费" > < / el-radio >
< / el-radio-group >
< / el-form-item >
< el-form-item size = "large" >
< el-button type = "primary" @click =" onSubmit " > 立即创建</ el-button >
< el-button > 取消< / el-button >
< / el-form-item >
< / el-form >
< script >
export default {
data() {
return {
sizeForm: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
2021-09-03 14:13:58 +08:00
desc: '',
},
}
2020-08-13 15:18:26 +08:00
},
methods: {
onSubmit() {
2021-09-03 14:13:58 +08:00
console.log('submit!')
},
},
}
2020-08-13 15:18:26 +08:00
< / script >
```
2021-09-03 14:13:58 +08:00
2020-08-13 15:18:26 +08:00
:::
### Form Attributes
2021-09-03 14:13:58 +08:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| ----------------------- | ----------------------------------------------------------------------------------------- | --------------- | --------------------- | ------ |
| model | 表单数据对象 | object | — | — |
| rules | 表单验证规则 | object | — | — |
| inline | 行内表单模式 | boolean | — | false |
| label-position | 表单域标签的位置,如果值为 left 或者 right 时,则需要设置 `label-width` | string | right / left / top | right |
| label-width | 表单域标签的宽度,例如 '50px'。作为 Form 直接子元素的 form-item 会继承该值。支持 `auto` 。 | string / number | — | — |
| label-suffix | 表单域标签的后缀 | string | — | — |
| hide-required-asterisk | 是否显示必填字段的标签旁边的红色星号 | boolean | — | false |
| show-message | 是否显示校验错误信息 | boolean | — | true |
| inline-message | 是否以行内形式展示校验信息 | boolean | — | false |
| status-icon | 是否在输入框中显示校验结果反馈图标 | boolean | — | false |
| validate-on-rule-change | 是否在 `rules` 属性改变后立即触发一次验证 | boolean | — | true |
| size | 用于控制该表单内组件的尺寸 | string | medium / small / mini | — |
| disabled | 是否禁用该表单内的所有组件。若设置为 true, 则表单内组件上的 disabled 属性不再生效 | boolean | — | false |
2020-08-13 15:18:26 +08:00
### Form Methods
2021-09-03 14:13:58 +08:00
| 方法名 | 说明 | 参数 |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| validate | 对整个表单进行校验的方法,参数为一个回调函数。该回调函数会在校验结束后被调用,并传入两个参数:是否校验成功和未通过校验的字段。若不传入回调函数,则会返回一个 promise | Function(callback: Function(boolean, object)) |
| validateField | 对部分表单字段进行校验的方法 | Function(props: array \| string, callback: Function(errorMessage: string)) |
| resetFields | 对整个表单进行重置,将所有字段值重置为初始值并移除校验结果 | — |
| scrollToField | 滚动到指定表单字段 | Function(prop: string) |
| clearValidate | 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组,如不传则移除整个表单的校验结果 | Function(props: array \| string) |
2020-08-13 15:18:26 +08:00
### Form Events
2021-09-03 14:13:58 +08:00
| 事件名称 | 说明 | 回调参数 |
| -------- | ---------------------- | ---------------------------------------------------------- |
2020-08-13 15:18:26 +08:00
| validate | 任一表单项被校验后触发 | 被校验的表单项 prop 值,校验是否通过,错误消息(如果存在) |
### Form-Item Attributes
2021-09-03 14:13:58 +08:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| -------------- | -------------------------------------------------------------------------------------------------------- | --------------- | --------------------------------- | ------ |
| prop | 表单域 model 字段,在使用 validate、resetFields 方法的情况下,该属性是必填的 | string | 传入 Form 组件的 `model` 中的字段 | — |
| label | 标签文本 | string | — | — |
| label-width | 表单域标签的的宽度,例如 '50px'。支持 `auto` 。 | string / number | — | — |
| required | 是否必填,如不设置,则会根据校验规则自动生成 | boolean | — | false |
| rules | 表单验证规则, 具体配置见下表, 更多内容参考[async-validator](https://github.com/yiminghe/async-validator) | object / array | — | — |
| error | 表单域验证错误信息, 设置该值会使表单验证状态变为`error`,并显示该错误信息 | string | — | — |
| show-message | 是否显示校验错误信息 | boolean | — | true |
| inline-message | 以行内形式展示校验信息 | boolean | — | false |
| size | 用于控制该表单域下组件的尺寸 | string | medium / small / mini | — |
2020-08-13 15:18:26 +08:00
2021-07-01 08:44:52 +08:00
### Rules
2021-09-03 14:13:58 +08:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| ------- | ------------ | ------ | ------------- | ------ |
| trigger | 验证触发方式 | string | blur / change | — |
2021-07-01 08:44:52 +08:00
2020-08-13 15:18:26 +08:00
### Form-Item Slot
2021-09-03 14:13:58 +08:00
| name | 说明 |
| ----- | ---------------------------------------------- |
| — | Form Item 的内容 |
| label | 自定义标签,参数为 { label } |
| error | 自定义表单校验信息的显示方式,参数为 { error } |
2020-08-13 15:18:26 +08:00
### Form-Item Methods
2021-09-03 14:13:58 +08:00
| 方法名 | 说明 | 参数 |
| ------------- | ---------------------------------------------------- | ---- |
| resetField | 对该表单项进行重置,将其值重置为初始值并移除校验结果 | — |
| clearValidate | 移除该表单项的校验结果 | — |