feat(@vben/web-antd): vxe-table)新增表单验证功能

- 在 vxe-table.ts 中添加 createRequiredValidation函数
- 在 data.ts 中使用 createRequiredValidation 替代原有的 className 函数
- 在 vxe-table 插件中添加 validation 相关的工具函数
- 优化表格列的验证逻辑,提高代码复用性和可维护性
pull/188/head
nehc 2025-08-02 23:28:59 +08:00
parent 5f56b14733
commit c64be886b4
4 changed files with 69 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import { IconifyIcon } from '@vben/icons';
import { $te } from '@vben/locales'; import { $te } from '@vben/locales';
import { import {
AsyncComponents, AsyncComponents,
createRequiredValidation,
setupVbenVxeTable, setupVbenVxeTable,
useVbenVxeGrid, useVbenVxeGrid,
} from '@vben/plugins/vxe-table'; } from '@vben/plugins/vxe-table';
@ -354,7 +355,7 @@ setupVbenVxeTable({
useVbenForm, useVbenForm,
}); });
export { useVbenVxeGrid }; export { createRequiredValidation, useVbenVxeGrid };
const [VxeTable, VxeColumn, VxeToolbar] = AsyncComponents; const [VxeTable, VxeColumn, VxeToolbar] = AsyncComponents;
export { VxeColumn, VxeTable, VxeToolbar }; export { VxeColumn, VxeTable, VxeToolbar };

View File

@ -1,6 +1,7 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { createRequiredValidation } from '#/adapter/vxe-table';
import { getSupplierSimpleList } from '#/api/erp/purchase/supplier'; import { getSupplierSimpleList } from '#/api/erp/purchase/supplier';
import { getSimpleUserList } from '#/api/system/user'; import { getSimpleUserList } from '#/api/system/user';
import { DICT_TYPE, getDictOptions } from '#/utils'; import { DICT_TYPE, getDictOptions } from '#/utils';
@ -108,22 +109,14 @@ export function useStockInItemTableColumns(
title: '仓库名称', title: '仓库名称',
minWidth: 150, minWidth: 150,
slots: { default: 'warehouseId' }, slots: { default: 'warehouseId' },
className: ({ row }: { row: any }) => { className: createRequiredValidation(isValidating, 'warehouseId'),
return isValidating?.value && !row.warehouseId
? 'required-field-error'
: '';
},
}, },
{ {
field: 'productId', field: 'productId',
title: '产品名称', title: '产品名称',
minWidth: 200, minWidth: 200,
slots: { default: 'productId' }, slots: { default: 'productId' },
className: ({ row }: { row: any }) => { className: createRequiredValidation(isValidating, 'productId'),
return isValidating?.value && !row.productId
? 'required-field-error'
: '';
},
}, },
{ {
field: 'stockCount', field: 'stockCount',
@ -145,11 +138,7 @@ export function useStockInItemTableColumns(
title: '数量', title: '数量',
minWidth: 120, minWidth: 120,
slots: { default: 'count' }, slots: { default: 'count' },
className: ({ row }: { row: any }) => { className: createRequiredValidation(isValidating, 'count'),
return isValidating?.value && (!row.count || row.count <= 0)
? 'required-field-error'
: '';
},
}, },
{ {
field: 'productPrice', field: 'productPrice',

View File

@ -1,8 +1,9 @@
export { AsyncComponents, setupVbenVxeTable } from './init'; export { AsyncComponents, setupVbenVxeTable } from './init';
export type { VxeTableGridOptions } from './types'; export type { VxeTableGridOptions } from './types';
export * from './use-vxe-grid'; export * from './use-vxe-grid';
export { default as VbenVxeGrid } from './use-vxe-grid.vue'; export { default as VbenVxeGrid } from './use-vxe-grid.vue';
export * from './validation';
export type { export type {
VxeGridListeners, VxeGridListeners,
VxeGridProps, VxeGridProps,

View File

@ -0,0 +1,61 @@
/**
*
* @param isValidating
* @param fieldName
* @param validationRules
* @returns className
*/
function createValidationClassName(
isValidating: any,
fieldName: string,
validationRules: ((row: any) => boolean) | string,
) {
return ({ row }: { row: any }) => {
if (!isValidating?.value) return '';
let isValid = true;
if (typeof validationRules === 'string') {
// 处理简单的验证规则
if (validationRules === 'required') {
isValid =
fieldName === 'count'
? row[fieldName] && row[fieldName] > 0
: !!row[fieldName];
}
} else if (typeof validationRules === 'function') {
// 处理自定义验证函数
isValid = validationRules(row);
}
return isValid ? '' : 'required-field-error';
};
}
/**
*
* @param isValidating
* @param fieldName
* @returns className
*/
function createRequiredValidation(isValidating: any, fieldName: string) {
return createValidationClassName(isValidating, fieldName, 'required');
}
/**
*
* @param isValidating
* @param validationFn
* @returns className
*/
function createCustomValidation(
isValidating: any,
validationFn: (row: any) => boolean,
) {
return createValidationClassName(isValidating, '', validationFn);
}
export {
createCustomValidation,
createRequiredValidation,
createValidationClassName,
};