diff --git a/web/src/lang/pages/en/crud/crud.ts b/web/src/lang/pages/en/crud/crud.ts index e7bb9dda..0da2845b 100644 --- a/web/src/lang/pages/en/crud/crud.ts +++ b/web/src/lang/pages/en/crud/crud.ts @@ -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', } diff --git a/web/src/lang/pages/zh-cn/crud/crud.ts b/web/src/lang/pages/zh-cn/crud/crud.ts index aad15d65..8f0a5b81 100644 --- a/web/src/lang/pages/zh-cn/crud/crud.ts +++ b/web/src/lang/pages/zh-cn/crud/crud.ts @@ -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} 不符合规范,请以 字母、_ 开头,不能出现 字母、数字、下划线 以外的字符', } diff --git a/web/src/utils/validate.ts b/web/src/utils/validate.ts index c3c03be6..7df7f7ac 100644 --- a/web/src/utils/validate.ts +++ b/web/src/utils/validate.ts @@ -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() diff --git a/web/src/views/backend/crud/design.vue b/web/src/views/backend/crud/design.vue index 24d97492..4aa3586b 100644 --- a/web/src/views/backend/crud/design.vue +++ b/web/src/views/backend/crud/design.vue @@ -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',