|
4 | 4 | title="Add Datasource" |
5 | 5 | width="500" |
6 | 6 | > |
7 | | - <el-form :model="form" label-width="auto"> |
8 | | - <el-form-item label="Name"> |
| 7 | + <el-form :model="form" label-width="auto" ref="dsFormRef" :rules="rules"> |
| 8 | + <el-form-item label="Name" prop="name"> |
9 | 9 | <el-input v-model="form.name" /> |
10 | 10 | </el-form-item> |
11 | 11 | <el-form-item label="Description"> |
|
40 | 40 | <div style="display: flex;justify-content: flex-end;"> |
41 | 41 | <el-button @click="close">Cancel</el-button> |
42 | 42 | <el-button @click="check">Test Connect</el-button> |
43 | | - <el-button type="primary" @click="save">Save</el-button> |
| 43 | + <el-button type="primary" @click="save(dsFormRef)">Save</el-button> |
44 | 44 | </div> |
45 | 45 | </el-form> |
46 | 46 | </el-dialog> |
47 | 47 | </template> |
48 | 48 | <script lang="ts" setup> |
49 | | -import { ref } from 'vue' |
| 49 | +import { ref, reactive } from 'vue' |
50 | 50 | import { datasourceApi } from '@/api/datasource' |
51 | 51 | import { encrypted, decrypted } from './js/aes' |
52 | 52 | import { ElMessage } from 'element-plus' |
| 53 | +import type { FormInstance, FormRules } from 'element-plus' |
53 | 54 |
|
| 55 | +const dsFormRef = ref<FormInstance>() |
54 | 56 | const emit = defineEmits(['refresh']) |
55 | 57 |
|
| 58 | +const rules = reactive<FormRules>({ |
| 59 | + name: [ |
| 60 | + { required: true, message: 'Please input name', trigger: 'blur' }, |
| 61 | + { min: 1, max: 50, message: 'Length should be 1 to 50', trigger: 'blur' }, |
| 62 | + ], |
| 63 | +}) |
| 64 | +
|
56 | 65 | const dialogVisible = ref<boolean>(false) |
57 | 66 | const dsType = [ |
58 | 67 | {label:"MySQL", value:"mysql"} |
@@ -109,27 +118,32 @@ const open = (item: any) => { |
109 | 118 | dialogVisible.value = true |
110 | 119 | } |
111 | 120 |
|
112 | | -const save = () => { |
113 | | - form.value.configuration = encrypted(JSON.stringify({ |
114 | | - host:config.value.host, |
115 | | - port:config.value.port, |
116 | | - username:config.value.username, |
117 | | - password:config.value.password, |
118 | | - database:config.value.database |
119 | | - })) |
120 | | - if (form.value.id) { |
121 | | - datasourceApi.update(form.value).then((res) => { |
122 | | - console.log(res) |
123 | | - close() |
124 | | - emit('refresh') |
125 | | - }) |
126 | | - } else { |
127 | | - datasourceApi.add(form.value).then((res: any) => { |
128 | | - console.log(res) |
129 | | - close() |
130 | | - emit('refresh') |
131 | | - }) |
132 | | - } |
| 121 | +const save = async(formEl: FormInstance | undefined) => { |
| 122 | + if(!formEl) return |
| 123 | + await formEl.validate((valid) => { |
| 124 | + if (valid) { |
| 125 | + form.value.configuration = encrypted(JSON.stringify({ |
| 126 | + host:config.value.host, |
| 127 | + port:config.value.port, |
| 128 | + username:config.value.username, |
| 129 | + password:config.value.password, |
| 130 | + database:config.value.database |
| 131 | + })) |
| 132 | + if (form.value.id) { |
| 133 | + datasourceApi.update(form.value).then((res) => { |
| 134 | + console.log(res) |
| 135 | + close() |
| 136 | + emit('refresh') |
| 137 | + }) |
| 138 | + } else { |
| 139 | + datasourceApi.add(form.value).then((res: any) => { |
| 140 | + console.log(res) |
| 141 | + close() |
| 142 | + emit('refresh') |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + }) |
133 | 147 | } |
134 | 148 |
|
135 | 149 | const check = () => { |
|
0 commit comments