element/packages/form/src/form.vue

78 lines
1.7 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
2016-08-29 11:19:14 +00:00
<form class="el-form" :class="[
labelPosition ? 'el-form--label-' + labelPosition : '',
{ 'el-form--inline': inline }
2016-07-27 06:15:02 +00:00
]">
<slot></slot>
</form>
</template>
<script>
export default {
name: 'ElForm',
componentName: 'form',
props: {
model: Object,
rules: Object,
2016-08-29 11:19:14 +00:00
labelPosition: String,
2016-07-27 06:15:02 +00:00
labelWidth: String,
labelSuffix: {
type: String,
default: ''
2016-08-29 11:19:14 +00:00
},
inline: Boolean
2016-07-27 06:15:02 +00:00
},
data() {
return {
fields: {},
2016-08-16 13:07:27 +00:00
fieldLength: 0
2016-07-27 06:15:02 +00:00
};
},
2016-08-16 13:07:27 +00:00
created() {
this.$on('el.form.addField', (field) => {
2016-07-27 06:15:02 +00:00
this.fields[field.prop] = field;
this.fieldLength++;
2016-08-16 13:07:27 +00:00
});
/* istanbul ignore next */
2016-08-16 13:07:27 +00:00
this.$on('el.form.removeField', (field) => {
if (this.fields[field.prop]) {
delete this.fields[field.prop];
this.fieldLength--;
}
2016-08-16 13:07:27 +00:00
});
2016-07-27 06:15:02 +00:00
},
methods: {
2016-08-16 13:07:27 +00:00
resetFields() {
2016-07-27 06:15:02 +00:00
for (let prop in this.fields) {
let field = this.fields[prop];
field.resetField();
}
},
validate(callback) {
var count = 0;
var valid = true;
for (let prop in this.fields) {
let field = this.fields[prop];
field.validate('', errors => {
if (errors) {
valid = false;
}
if (++count === this.fieldLength) {
callback(valid);
}
});
}
},
validateField(prop, cb) {
var field = this.fields[prop];
if (!field) { throw new Error('must call validateField with valid prop string!'); }
field.validate('', cb);
}
}
};
</script>