优化库存生成表单的数字类型处理和验证逻辑
This commit is contained in:
@@ -178,6 +178,7 @@
|
||||
v-model="stockForm[field.name]"
|
||||
:min="field.min"
|
||||
:max="field.max"
|
||||
:controls="!field.maxLength"
|
||||
controls-position="right"
|
||||
style="width: 200px"
|
||||
/>
|
||||
@@ -558,10 +559,15 @@ const onGenerateStock = async (row: any) => {
|
||||
const res = await getStockFormFields(row.id);
|
||||
stockFormFields.value = res.data.fields || [];
|
||||
|
||||
// 设置默认值
|
||||
// 设置默认值,根据字段类型转换
|
||||
stockFormFields.value.forEach((field) => {
|
||||
if (field.default !== undefined) {
|
||||
stockForm[field.name] = field.default;
|
||||
// 如果是数字类型,确保默认值也是数字
|
||||
if (field.type === 'number') {
|
||||
stockForm[field.name] = Number(field.default);
|
||||
} else {
|
||||
stockForm[field.name] = field.default;
|
||||
}
|
||||
} else if (field.type === 'number') {
|
||||
stockForm[field.name] = field.min || 0;
|
||||
} else {
|
||||
@@ -586,10 +592,20 @@ const onSubmitStock = async () => {
|
||||
if (valid) {
|
||||
stockSubmitLoading.value = true;
|
||||
try {
|
||||
await stockOperation({
|
||||
// 根据字段类型转换数据
|
||||
const submitData: Record<string, any> = {
|
||||
assetSkuId: currentSkuId.value,
|
||||
...stockForm,
|
||||
};
|
||||
stockFormFields.value.forEach((field) => {
|
||||
const value = stockForm[field.name];
|
||||
if (field.type === 'number' && value !== undefined && value !== '') {
|
||||
submitData[field.name] = Number(value);
|
||||
} else if (value !== undefined && value !== '') {
|
||||
submitData[field.name] = value;
|
||||
}
|
||||
});
|
||||
|
||||
await stockOperation(submitData as any);
|
||||
ElMessage.success('库存生成成功');
|
||||
stockFormVisible.value = false;
|
||||
getSkuList();
|
||||
@@ -610,10 +626,34 @@ const getStockFormRules = () => {
|
||||
if (field.required) {
|
||||
fieldRules.push({ required: true, message: `${field.label}不能为空`, trigger: 'blur' });
|
||||
}
|
||||
if (field.type === 'number' && field.min !== undefined) {
|
||||
fieldRules.push({ type: 'number', min: field.min, message: `${field.label}最小值为${field.min}`, trigger: 'blur' });
|
||||
}
|
||||
if (field.maxLength) {
|
||||
if (field.type === 'number') {
|
||||
// 数字类型的最小值和最大值验证
|
||||
if (field.min !== undefined) {
|
||||
fieldRules.push({ type: 'number', min: field.min, message: `${field.label}最小值为${field.min}`, trigger: 'blur' });
|
||||
}
|
||||
if (field.max !== undefined) {
|
||||
fieldRules.push({ type: 'number', max: field.max, message: `${field.label}最大值为${field.max}`, trigger: 'blur' });
|
||||
}
|
||||
// 数字位数验证
|
||||
if (field.maxLength) {
|
||||
fieldRules.push({
|
||||
validator: (_rule: any, value: any, callback: any) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
const strValue = String(value);
|
||||
if (strValue.length > field.maxLength!) {
|
||||
callback(new Error(`${field.label}不能超过${field.maxLength}位`));
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
},
|
||||
trigger: 'blur',
|
||||
});
|
||||
}
|
||||
} else if (field.maxLength) {
|
||||
// 字符串类型的最大长度验证
|
||||
fieldRules.push({ max: field.maxLength, message: `${field.label}最大长度为${field.maxLength}`, trigger: 'blur' });
|
||||
}
|
||||
if (fieldRules.length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user