element/packages/form/src/form.vue

75 lines
1.6 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<form :class="[
type ? 'el-form-' + type : 'el-form',
{ 'is-label-left': labelAlign === 'left' }
]">
<slot></slot>
</form>
</template>
<script>
export default {
name: 'ElForm',
componentName: 'form',
props: {
model: Object,
rules: Object,
type: String,
labelAlign: String,
labelWidth: String,
labelSuffix: {
type: String,
default: ''
}
},
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
});
this.$on('el.form.removeField', (field) => {
2016-07-27 06:15:02 +00:00
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>