feat(CRUD):增加字段名称检查

This commit is contained in:
妙码生花 2023-01-01 14:00:10 +08:00
parent f5ec3e3849
commit bff475dc72
4 changed files with 26 additions and 3 deletions

View File

@ -113,4 +113,6 @@ export default {
'If it is left blank, the model of the associated table will be generated automatically If the table already has a model, it is recommended to select it to avoid repeated generation',
'The field comment will be used as the CRUD dictionary, and will be identified as the field title before the colon, and as the data dictionary after the colon':
'The field comment will be used as the CRUD dictionary, and will be identified as the field title before the colon, and as the data dictionary after the colon',
'Field name is invalid It starts with a letter or underscore and cannot contain any character other than letters, digits, or underscores':
'Field name {field} is invalid. It starts with a letter or underscore and cannot contain any character other than letters, digits, or underscores',
}

View File

@ -112,4 +112,6 @@ export default {
'留空则自动生成关联表的模型,若该表已有模型,建议选择好以免重复生成',
'The field comment will be used as the CRUD dictionary, and will be identified as the field title before the colon, and as the data dictionary after the colon':
'字段注释将作为 CRUD字典冒号前将识别为字段标题冒号后识别为数据字典',
'Field name is invalid It starts with a letter or underscore and cannot contain any character other than letters, digits, or underscores':
'字段名 {field} 不符合规范,请以 字母、_ 开头,不能出现 字母、数字、下划线 以外的字符',
}

View File

@ -48,11 +48,14 @@ export function validatorPassword(rule: any, val: string, callback: Function) {
/**
*
*/
export function regularVarName(val: string) {
return /^([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*$/.test(val)
}
export function validatorVarName(rule: any, val: string, callback: Function) {
if (!val) {
return callback()
}
if (!/^([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*$/.test(val)) {
if (!regularVarName(val)) {
return callback(new Error(i18n.global.t('validate.Please enter the correct name')))
}
return callback()

View File

@ -494,7 +494,7 @@ import { changeStep, state as crudState, getTableAttr } from '/@/views/backend/c
import { ElNotification, FormItemRule, FormInstance, ElMessageBox } from 'element-plus'
import { getDatabaseList, getFileData, generateCheck, generate, parseFieldData, postLogStart } from '/@/api/backend/crud'
import { getTableFieldList } from '/@/api/common'
import { buildValidatorData } from '/@/utils/validate'
import { buildValidatorData, regularVarName } from '/@/utils/validate'
import { getArrayKey } from '/@/utils/common'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
@ -706,13 +706,29 @@ const startGenerate = () => {
const onGenerate = () => {
let msg = ''
if (!state.table.name) msg = t('crud.crud.Please enter the data table name!')
//
state.fields.find((item) => {
if (!regularVarName(item.name)) {
msg = t(
'crud.crud.Field name is invalid It starts with a letter or underscore and cannot contain any character other than letters, digits, or underscores',
{ field: item.name }
)
return true
}
})
//
const pkIndex = state.fields.findIndex((item) => {
return item.primaryKey
})
if (pkIndex === -1) {
msg = t('crud.crud.Please design the primary key field!')
}
//
if (!state.table.name) msg = t('crud.crud.Please enter the data table name!')
if (msg) {
ElNotification({
type: 'error',