fix: disable illegal characters for renaming connection group

This commit is contained in:
Lykin 2023-11-28 10:53:48 +08:00
parent 9671f89770
commit f0e54f280c
2 changed files with 56 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import { computed, reactive, ref, watch } from 'vue'
import useDialog from 'stores/dialog' import useDialog from 'stores/dialog'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import useConnectionStore from 'stores/connections.js' import useConnectionStore from 'stores/connections.js'
import { isEmpty } from 'lodash' import { every, get, includes, isEmpty } from 'lodash'
/** /**
* Dialog for create or rename group * Dialog for create or rename group
@ -13,6 +13,24 @@ const editGroup = ref('')
const groupForm = reactive({ const groupForm = reactive({
name: '', name: '',
}) })
const groupFormRef = ref(null)
const formRules = computed(() => {
const requiredMsg = i18n.t('dialogue.field_required')
const illegalChars = ['/', '\\']
return {
name: [
{ required: true, message: requiredMsg, trigger: 'input' },
{
validator: (rule, value) => {
return every(illegalChars, (c) => !includes(value, c))
},
message: i18n.t('dialogue.illegal_characters'),
trigger: 'input',
},
],
}
})
const isRenameMode = computed(() => !isEmpty(editGroup.value)) const isRenameMode = computed(() => !isEmpty(editGroup.value))
@ -30,6 +48,13 @@ watch(
const i18n = useI18n() const i18n = useI18n()
const onConfirm = async () => { const onConfirm = async () => {
try { try {
await groupFormRef.value?.validate((errs) => {
const err = get(errs, '0.0.message')
if (err != null) {
$message.error(err)
}
})
const { name } = groupForm const { name } = groupForm
if (isRenameMode.value) { if (isRenameMode.value) {
const { success, msg } = await connectionStore.renameGroup(editGroup.value, name) const { success, msg } = await connectionStore.renameGroup(editGroup.value, name)
@ -47,9 +72,11 @@ const onConfirm = async () => {
} }
} }
} catch (e) { } catch (e) {
$message.error(e.message) const msg = get(e, 'message')
if (!isEmpty(msg)) {
$message.error(msg)
}
} }
onClose()
} }
const onClose = () => { const onClose = () => {
@ -77,8 +104,14 @@ const onClose = () => {
transform-origin="center" transform-origin="center"
@positive-click="onConfirm" @positive-click="onConfirm"
@negative-click="onClose"> @negative-click="onClose">
<n-form :model="groupForm" :show-label="false" :show-require-mark="false" label-placement="top"> <n-form
<n-form-item :label="$t('dialogue.group.name')" required> ref="groupFormRef"
:model="groupForm"
:rules="formRules"
:show-label="false"
:show-require-mark="false"
label-placement="top">
<n-form-item :label="$t('dialogue.group.name')" path="name" required>
<n-input v-model:value="groupForm.name" placeholder="" /> <n-input v-model:value="groupForm.name" placeholder="" />
</n-form-item> </n-form-item>
</n-form> </n-form>

View File

@ -2,7 +2,7 @@
import { computed, h, reactive, ref, watch } from 'vue' import { computed, h, reactive, ref, watch } from 'vue'
import { types, typesColor } from '@/consts/support_redis_type.js' import { types, typesColor } from '@/consts/support_redis_type.js'
import useDialog from 'stores/dialog' import useDialog from 'stores/dialog'
import { isEmpty, keys, map } from 'lodash' import { get, isEmpty, keys, map } from 'lodash'
import NewStringValue from '@/components/new_value/NewStringValue.vue' import NewStringValue from '@/components/new_value/NewStringValue.vue'
import NewHashValue from '@/components/new_value/NewHashValue.vue' import NewHashValue from '@/components/new_value/NewHashValue.vue'
import NewListValue from '@/components/new_value/NewListValue.vue' import NewListValue from '@/components/new_value/NewListValue.vue'
@ -103,13 +103,20 @@ const renderTypeLabel = (option) => {
const browserStore = useBrowserStore() const browserStore = useBrowserStore()
const tabStore = useTabStore() const tabStore = useTabStore()
const onAdd = async () => { const onAdd = async () => {
await newFormRef.value?.validate().catch((err) => { await newFormRef.value?.validate((errs) => {
$message.error(err.message) const err = get(errs, '0.0.message')
if (err != null) {
$message.error(err)
}
})
await subFormRef.value?.validate((errs) => {
const err = get(errs, '0.0.message')
if (err != null) {
$message.error(err)
} else {
$message.error(i18n.t('dialogue.spec_field_required', { key: i18n.t('dialogue.field.element') }))
}
}) })
if (subFormRef.value?.validate && !subFormRef.value?.validate()) {
$message.error(i18n.t('dialogue.spec_field_required', { key: i18n.t('dialogue.field.element') }))
return false
}
try { try {
const { server, db, key, type, ttl } = newForm const { server, db, key, type, ttl } = newForm
let { value } = newForm let { value } = newForm
@ -132,7 +139,10 @@ const onAdd = async () => {
$message.error(msg) $message.error(msg)
} }
dialogStore.closeNewKeyDialog() dialogStore.closeNewKeyDialog()
} catch (e) {} } catch (e) {
return false
}
return true
} }
const onClose = () => { const onClose = () => {