|
|
@@ -40,6 +40,9 @@
|
|
|
</template>
|
|
|
<script lang="ts" setup>
|
|
|
import { reactive } from 'vue';
|
|
|
+import { ElMessage, dayjs } from 'element-plus';
|
|
|
+import AsyncValidator from 'async-validator';
|
|
|
+import { isDate, isString } from 'lodash-es';
|
|
|
const props = defineProps({
|
|
|
value: {
|
|
|
type: Array<any>,
|
|
|
@@ -49,6 +52,9 @@ const props = defineProps({
|
|
|
type: Array<any>,
|
|
|
default: () => [],
|
|
|
},
|
|
|
+ rules: {
|
|
|
+ type: Object,
|
|
|
+ },
|
|
|
params: {
|
|
|
type: Object,
|
|
|
},
|
|
|
@@ -75,7 +81,40 @@ function del(record: any, index: number) {
|
|
|
emit('update:value', vm.value);
|
|
|
emit('delete', { value: vm.value, record });
|
|
|
}
|
|
|
-function add() {
|
|
|
+async function add() {
|
|
|
+ let msgs: string[] = [];
|
|
|
+
|
|
|
+ for (const field in props.rules) {
|
|
|
+ let rule = props.rules[field];
|
|
|
+
|
|
|
+ let val = vm.formData[field];
|
|
|
+ if(val){
|
|
|
+ if(isDate(val) ){
|
|
|
+ val=dayjs(val).format(props.columns.filter(m=>m.field==field)[0]
|
|
|
+ .format|| 'YYYY-MM-DD');
|
|
|
+ vm.formData[field]=val;
|
|
|
+ }else if (!isString(val)) {
|
|
|
+ val= val.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const validator = new AsyncValidator({
|
|
|
+ [field]: rule,
|
|
|
+ });
|
|
|
+ await validator.validate({ [field]: val }, { firstFields: true }).catch((error: any) => {
|
|
|
+ var _a, _b;
|
|
|
+ const { errors, fields } = error;
|
|
|
+ if (!errors || !fields) {
|
|
|
+ console.error(error);
|
|
|
+ }
|
|
|
+ if (errors) {
|
|
|
+ msgs.push((_b = (_a = errors == null ? void 0 : errors[0]) == null ? void 0 : _a.message) != null ? _b : `${field} 必填!`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (msgs.length > 0) {
|
|
|
+ ElMessage.error(msgs.join('。'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
for (const key in props.params) {
|
|
|
vm.formData[key] = props.params[key];
|
|
|
}
|