element/examples/docs/zh-cn/form.md

816 lines
23 KiB
Markdown
Raw Normal View History

2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
2016-08-16 21:07:27 +08:00
var checkAge = (rule, value, callback) => {
var age = parseInt(value, 10);
setTimeout(() => {
if (!Number.isInteger(age)) {
callback(new Error('请输入数字值'));
} else{
if (age < 18) {
callback(new Error('必须年满18岁'));
} else {
callback();
}
}
}, 1000);
};
var validaePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.ruleForm2.checkPass !== '') {
this.$refs.ruleForm2.validateField('checkPass');
}
callback();
}
};
var validaePass2 = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.ruleForm2.pass) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
2016-07-27 14:15:02 +08:00
return {
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
},
formInline: {
user: '',
region: ''
},
formStacked: {
name: '',
region: '',
type: '',
remark: ''
},
2016-08-29 19:19:14 +08:00
formAlignRight: {
name: '',
region: '',
type: ''
},
2016-07-27 14:15:02 +08:00
formAlignLeft: {
name: '',
region: '',
2016-08-29 19:19:14 +08:00
type: ''
2016-07-27 14:15:02 +08:00
},
ruleForm: {
2016-08-29 19:19:14 +08:00
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
2016-07-27 14:15:02 +08:00
},
ruleForm2: {
pass: '',
checkPass: '',
age: ''
},
formLabelWidth: '80px',
options: [
],
rules: {
2016-08-29 19:19:14 +08:00
name: [
2016-10-13 11:14:02 +08:00
{ required: true, message: '请输入活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
region: [
{ required: true, message: '请选择活动区域', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
date1: [
{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
date2: [
{ type: 'date', required: true, message: '请选择时间', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
type: [
{ type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
resource: [
{ required: true, message: '请选择活动资源', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
desc: [
2016-08-29 19:19:14 +08:00
{ required: true, message: '请填写活动形式', trigger: 'blur' }
2016-07-27 14:15:02 +08:00
]
},
rules2: {
pass: [
{ required: true, message: '请输入密码', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: validaePass }
2016-07-27 14:15:02 +08:00
],
checkPass: [
{ required: true, message: '请再次输入密码', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: validaePass2 }
2016-07-27 14:15:02 +08:00
],
age: [
{ required: true, message: '请填写年龄', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: checkAge, trigger: 'change' }
2016-07-27 14:15:02 +08:00
]
},
dynamicForm: {
2016-08-16 21:07:27 +08:00
domains: [{
key: 1,
value: ''
}],
2016-07-27 14:15:02 +08:00
email: ''
},
dynamicRule: {
email: [
{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
]
}
};
},
methods: {
handleSubmit(ev) {
this.$refs.ruleForm.validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
handleSubmit2(ev) {
this.$refs.ruleForm2.validate(valid => {
if (valid) {
alert('申请已提交');
} else {
console.log('error submit!!');
return false;
}
});
},
handleSubmit3(ev) {
this.$refs.dynamicForm.validate(valid => {
if (valid) {
alert('申请已提交');
} else {
console.log('error submit!!');
return false;
}
});
},
handleReset() {
2016-08-16 21:07:27 +08:00
this.$refs.ruleForm.resetFields();
2016-07-27 14:15:02 +08:00
},
handleReset2() {
2016-08-16 21:07:27 +08:00
this.$refs.ruleForm2.resetFields();
2016-07-27 14:15:02 +08:00
},
handleValidate(prop, errorMsg) {
console.log(prop, errorMsg);
},
onSubmit() {
console.log('submit!');
},
onRuleFormSubmit() {
console.log('onRuleFormSubmit');
2016-08-16 21:07:27 +08:00
},
removeDomain(item) {
var index = this.dynamicForm.domains.indexOf(item)
if (index !== -1) {
this.dynamicForm.domains.splice(index, 1)
}
},
addDomain() {
this.dynamicForm.domains.push({
key: this.dynamicForm.domains.length,
value: ''
});
2016-07-27 14:15:02 +08:00
}
}
}
</script>
<style>
.demo-form {
2016-09-01 14:40:41 +08:00
.el-select .el-input {
width: 360px;
}
2016-08-29 19:19:14 +08:00
.el-form {
width: 440px;
2016-07-27 14:15:02 +08:00
}
.line {
text-align: center;
}
.el-checkbox-group {
width: 320px;
margin: 0;
padding: 0;
list-style: none;
&:after,&:before {
content: ' ';
display: table;
}
&:after {
clear: both;
visibility: hidden;
font-size: 0;
height: 0;
}
.el-checkbox {
float: left;
width: 160px;
padding-right: 20px;
margin: 0;
padding: 0;
+ .el-checkbox {
margin-left: 0;
}
}
}
2016-08-29 19:19:14 +08:00
.demo-form-normal {
width: 440px;
2016-07-27 14:15:02 +08:00
}
2016-08-29 19:19:14 +08:00
.demo-form-inline {
.el-input {
width: 150px;
}
> * {
margin-right: 10px;
}
2016-07-27 14:15:02 +08:00
}
2016-08-29 19:19:14 +08:00
.demo-form-stacked {
width: 270px;
.el-select .el-input {
width: 100%;
}
2016-07-27 14:15:02 +08:00
}
2016-08-29 19:19:14 +08:00
.demo-ruleForm {
width: 460px;
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
.el-input,
.el-textarea {
width: auto;
}
2016-09-01 14:40:41 +08:00
.el-select .el-input {
width: 360px;
}
2016-07-27 14:15:02 +08:00
}
2016-08-29 19:19:14 +08:00
.demo-dynamic {
.el-input {
display: inline-block;
margin-right: 10px;
width: 270px;
2016-10-14 23:47:26 +08:00
vertical-align: top;
2016-08-29 19:19:14 +08:00
}
}
.fr {
float: right;
2016-07-27 14:15:02 +08:00
}
}
</style>
2016-08-29 19:19:14 +08:00
## Form 表单
由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 典型表单
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
包括各种表单项,比如输入框、选择器、开关、单选框、多选框等。
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo 在 Form 组件中,每一个表单域由一个 Form-Item 组件构成,表单域中可以放置各种类型的表单控件,包括 Input、Select、Checkbox、Radio、Switch、DatePicker、TimePicker
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form ref="form" :model="form" label-width="80px" @submit.prevent="onSubmit">
<el-form-item label="活动名称">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="活动区域">
2016-09-01 14:40:41 +08:00
<el-select v-model="form.region" placeholder="请选择活动区域">
2016-08-29 19:19:14 +08:00
<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">
<el-date-picker type="date" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-time-picker type="fixed-time" placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
</el-col>
</el-form-item>
<el-form-item label="即时配送">
<el-switch on-text="" off-text="" 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">立即创建</el-button>
<el-button @click.native.prevent>取消</el-button>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
}
}
},
methods: {
onSubmit() {
console.log('submit!');
}
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 行内表单
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
当垂直方向空间受限且表单较简单时,可以在一行内放置表单。
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo Form 组件的 `type` 属性可以控制表单的类型,当设为 `inline` 时可以让表单域变为行内的表单域
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :inline="true" :model="formInline" @submit.prevent="onSubmit" class="demo-form-inline">
<el-form-item>
<el-input v-model="formInline.user" placeholder="审批人"></el-input>
</el-form-item><el-form-item>
2016-09-01 14:40:41 +08:00
<el-select v-model="formInline.region" placeholder="活动区域">
2016-08-29 19:19:14 +08:00
<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">查询</el-button>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
formInline: {
user: '',
region: ''
}
}
},
methods: {
onSubmit() {
console.log('submit!');
}
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 对齐方式
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
根据具体目标和制约因素,选择最佳的标签对齐方式。
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
顶部对齐
::: demo 通过设置 `label-position` 属性可以改变表单域标签的位置,可选值为 `top`、`left`,当设为 `top` 时标签会置于表单域的顶部
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form label-position="top" :model="formStacked" class="demo-form-stacked">
<el-form-item label="名称">
<el-input v-model="formStacked.name"></el-input>
</el-form-item>
<el-form-item label="活动区域">
<el-input v-model="formStacked.region"></el-input>
</el-form-item>
<el-form-item label="活动展开形式">
<el-input v-model="formStacked.type"></el-input>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
formStacked: {
name: '',
region: '',
2016-08-29 19:19:14 +08:00
type: ''
2016-07-27 14:15:02 +08:00
}
};
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
右对齐
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo 通过设置 `label-position` 属性可以改变表单域标签的位置,默认不设置的情况下标签是右对齐的
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :model="formAlignRight" label-width="80px">
<el-form-item label="活动名称">
<el-input v-model="formAlignRight.name"></el-input>
</el-form-item>
<el-form-item label="推广地">
<el-input v-model="formAlignRight.region"></el-input>
</el-form-item>
<el-form-item label="活动形式">
<el-input v-model="formAlignRight.type"></el-input>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
2016-08-29 19:19:14 +08:00
formAlignRight: {
2016-07-27 14:15:02 +08:00
name: '',
region: '',
type: ''
}
};
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
左对齐
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo 通过设置 `label-position` 属性可以改变表单域标签的位置,可选值为 `top`、`left`,当设为 `left` 时标签会变为左对齐
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :model="formAlignLeft" label-position="left" @submit.prevent="onSubmit" label-width="80px">
<el-form-item label="活动名称">
<el-input v-model="formAlignLeft.name"></el-input>
</el-form-item>
<el-form-item label="推广地">
<el-input v-model="formAlignLeft.region"></el-input>
</el-form-item>
<el-form-item label="活动形式">
<el-input v-model="formAlignLeft.type"></el-input>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
2016-08-29 19:19:14 +08:00
formAlignLeft: {
name: '',
region: '',
type: ''
2016-07-27 14:15:02 +08:00
}
};
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 表单验证
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
在防止用户犯错的前提下,尽可能让用户更早地发现并纠正错误。
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo Form 组件提供了表单验证的功能,只需要通过 `rule` 属性传入约定的验证规则,并 Form-Item 的 `prop` 属相设置为需校验的字段名即可。校验规则参见 [async-validator](https://github.com/yiminghe/async-validator)
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="活动名称" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item label="活动区域" prop="region">
2016-09-01 14:40:41 +08:00
<el-select v-model="ruleForm.region" placeholder="请选择活动区域">
2016-08-29 19:19:14 +08:00
<el-option label="区域一" value="shanghai"></el-option>
<el-option label="区域二" value="beijing"></el-option>
</el-select>
</el-form-item>
2016-10-12 13:51:33 +08:00
<el-form-item label="活动时间" required>
2016-08-29 19:19:14 +08:00
<el-col :span="11">
<el-form-item prop="date1">
<el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
</el-form-item>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-form-item prop="date2">
<el-time-picker type="fixed-time" placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
</el-form-item>
</el-col>
</el-form-item>
<el-form-item label="即时配送">
<el-switch on-text="" off-text="" 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>
<el-button type="primary" @click.native.prevent="handleSubmit">立即创建</el-button>
<el-button @click.native.prevent="handleReset">重置</el-button>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
ruleForm: {
2016-08-29 19:19:14 +08:00
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
2016-07-27 14:15:02 +08:00
},
rules: {
2016-08-29 19:19:14 +08:00
name: [
{ required: true, message: '请输入活动名称', trigger: 'blur' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
region: [
{ required: true, message: '请选择活动区域', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
date1: [
{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }
2016-08-16 21:07:27 +08:00
],
2016-08-29 19:19:14 +08:00
date2: [
{ type: 'date', required: true, message: '请选择时间', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
type: [
{ type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
2016-08-29 19:19:14 +08:00
resource: [
{ required: true, message: '请选择活动资源', trigger: 'change' }
2016-07-27 14:15:02 +08:00
],
desc: [
2016-08-29 19:19:14 +08:00
{ required: true, message: '请填写活动形式', trigger: 'blur' }
2016-07-27 14:15:02 +08:00
]
}
};
},
methods: {
2016-08-16 21:07:27 +08:00
handleReset() {
this.$refs.ruleForm.resetFields();
},
2016-07-27 14:15:02 +08:00
handleSubmit(ev) {
this.$refs.ruleForm.validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 自定义校验规则
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo 这个例子中展示了如何使用自定义验证规则来完成密码的二次验证
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
<el-form-item label="密码" prop="pass">
<el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="ruleForm2.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click.native.prevent="handleSubmit2">提交</el-button>
<el-button @click.native.prevent="handleReset2">重置</el-button>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
2016-08-16 21:07:27 +08:00
var checkAge = (rule, value, callback) => {
var age = parseInt(value, 10);
setTimeout(() => {
if (!Number.isInteger(age)) {
callback(new Error('请输入数字值'));
} else{
if (age < 18) {
callback(new Error('必须年满18岁'));
} else {
callback();
}
}
}, 1000);
};
var validaePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.ruleForm2.checkPass !== '') {
this.$refs.ruleForm2.validateField('checkPass');
}
callback();
}
};
var validaePass2 = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.ruleForm2.pass) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
2016-07-27 14:15:02 +08:00
return {
ruleForm2: {
pass: '',
checkPass: '',
age: ''
},
rules2: {
pass: [
{ required: true, message: '请输入密码', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: validaePass }
2016-07-27 14:15:02 +08:00
],
checkPass: [
{ required: true, message: '请再次输入密码', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: validaePass2 }
2016-07-27 14:15:02 +08:00
],
age: [
{ required: true, message: '请填写年龄', trigger: 'blur' },
2016-08-16 21:07:27 +08:00
{ validator: checkAge, trigger: 'change' }
2016-07-27 14:15:02 +08:00
]
}
};
},
methods: {
2016-08-16 21:07:27 +08:00
handleReset2() {
this.$refs.ruleForm2.resetFields();
},
handleSubmit2(ev) {
this.$refs.ruleForm2.validate((valid) => {
2016-07-27 14:15:02 +08:00
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### 动态增减表单项
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
::: demo 除了在 Form 组件上一次性传递所有的验证规则外还可以在单个的表单域上传递属性的验证规则
2016-07-27 14:15:02 +08:00
```html
2016-08-29 19:19:14 +08:00
<el-form :model="dynamicForm" :rules="dynamicRule" ref="dynamicForm" label-width="100px" class="demo-dynamic">
<el-form-item prop="email" label="邮箱">
<el-input v-model="dynamicForm.email"></el-input>
</el-form-item>
<el-form-item
v-for="(domain, index) in dynamicForm.domains"
:label="'域名' + index"
:key="domain.key"
:prop="'domains:' + index"
:rules="{
type: 'object', required: true,
fields: {
value: { required: true, message: '域名不能为空', trigger: 'blur' }
}
}"
>
<el-input v-model="domain.value"></el-input><el-button @click.native.prevent="removeDomain(domain)">删除</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click.native.prevent="handleSubmit3">提交</el-button>
<el-button @click.native.prevent="addDomain">新增域名</el-button>
</el-form-item>
</el-form>
2016-07-27 14:15:02 +08:00
<script>
export default {
data() {
return {
dynamicForm: {
2016-08-16 21:07:27 +08:00
domains: [{
key: 1,
value: ''
}],
2016-07-27 14:15:02 +08:00
email: ''
},
dynamicRule: {
email: [
{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
]
}
};
},
methods: {
handleSubmit3(ev) {
this.$refs.ruleForm.validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
2016-08-16 21:07:27 +08:00
},
removeDomain(item) {
var index = this.dynamicForm.domains.indexOf(item)
if (index !== -1) {
this.dynamicForm.domains.splice(index, 1)
}
},
addDomain() {
this.dynamicForm.domains.push({
key: this.dynamicForm.domains.length,
value: ''
});
2016-07-27 14:15:02 +08:00
}
}
}
</script>
```
2016-08-29 19:19:14 +08:00
:::
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### Form Attributes
2016-07-27 14:15:02 +08:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---------- |-------------- |---------- |-------------------------------- |-------- |
2016-08-29 19:19:14 +08:00
| model | 表单数据对象 | object | — | — |
| rules | 表单验证规则 | object | — | — |
2016-07-27 14:15:02 +08:00
| type | 表单类型 | string | stacked, inline, horizontal | horizontal |
| label-align | 表单域标签的水平对齐位置 | string | right,left | right |
2016-08-29 19:19:14 +08:00
| label-width | 表单域标签的宽度,所有的 form-item 都会继承 form 组件的 labelWidth 的值 | string | — | — |
| label-suffix | 表单域标签的后缀 | string | — | — |
### Form Methods
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
| 方法名 | 说明 |
|---------- |-------------- |
| validate(cb) | 对整个表单进行校验的方法 |
| validateField(prop, cb) | 对部分表单字段进行校验的方法 |
| resetFields | 对整个表单进行重置,将所有字段值重置为空并移除校验结果 |
2016-07-27 14:15:02 +08:00
2016-08-29 19:19:14 +08:00
### Form-Item Attributes
2016-07-27 14:15:02 +08:00
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---------- |-------------- |---------- |-------------------------------- |-------- |
2016-08-29 19:19:14 +08:00
| prop | 表单域 model 字段 | string | 传入 Form 组件的 `model` 中的字段 | — |
| label | 标签文本 | string | — | — |
| label-width | 表单域标签的的宽度,例如 '50px' | string | — | — |
| required | 是否必填,如不设置,则会根据校验规则自动生成 | bolean | — | false |