Skip to content

Commit bb1b267

Browse files
committed
fix(form): fix form verification and console error message issues
1 parent e04aaa0 commit bb1b267

File tree

7 files changed

+27
-19
lines changed

7 files changed

+27
-19
lines changed

CHANGELOG.zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
- 修复 tree 文本超出挡住操作按钮问题
2525
- 修复通过 useRedo 刷新页面参数丢失问题
26+
- 修复表单校验先设置在校验及控制台错误信息问题
2627

2728
### 🎫 Chores
2829

src/components/Form/src/BasicForm.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@
7171
const schemaRef = ref<Nullable<FormSchema[]>>(null);
7272
const formElRef = ref<Nullable<FormActionType>>(null);
7373
74-
const getRowWrapStyleRef = computed((): any => {
75-
const { baseRowStyle } = unref(getProps);
76-
return baseRowStyle || {};
77-
});
78-
7974
const getMergePropsRef = computed(
8075
(): FormProps => {
8176
return deepMerge(cloneDeep(props), unref(propsRef));
8277
}
8378
);
8479
80+
const getRowWrapStyleRef = computed((): any => {
81+
const { baseRowStyle } = unref(getMergePropsRef);
82+
return baseRowStyle || {};
83+
});
84+
8585
// 获取表单基本配置
8686
const getProps = computed(
8787
(): FormProps => {
@@ -103,7 +103,7 @@
103103
const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any);
104104
for (const schema of schemas) {
105105
const { defaultValue, component } = schema;
106-
if (defaultValue && dateItemType.includes(component!)) {
106+
if (defaultValue && dateItemType.includes(component)) {
107107
if (!Array.isArray(defaultValue)) {
108108
schema.defaultValue = moment(defaultValue);
109109
} else {

src/components/Form/src/FormItem.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { upperFirst, cloneDeep } from 'lodash-es';
1717
import { useItemLabelWidth } from './hooks/useLabelWidth';
1818
import { ComponentType } from './types';
1919
import { isNumber } from '/@/utils/is';
20+
import { useI18n } from '/@/hooks/web/useI18n';
2021

2122
export default defineComponent({
2223
name: 'BasicFormItem',
@@ -46,6 +47,8 @@ export default defineComponent({
4647
},
4748
},
4849
setup(props, { slots }) {
50+
const { t } = useI18n('component.form');
51+
// @ts-ignore
4952
const itemLabelWidthRef = useItemLabelWidth(toRef(props, 'schema'), toRef(props, 'formProps'));
5053

5154
const getValuesRef = computed(() => {
@@ -132,7 +135,7 @@ export default defineComponent({
132135
let rules: ValidationRule[] = cloneDeep(defRules) as ValidationRule[];
133136

134137
if ((!rules || rules.length === 0) && required) {
135-
rules = [{ required }];
138+
rules = [{ required, type: 'string' }];
136139
}
137140

138141
const requiredRuleIndex: number = rules.findIndex(
@@ -142,6 +145,9 @@ export default defineComponent({
142145
if (requiredRuleIndex !== -1) {
143146
const rule = rules[requiredRuleIndex];
144147
if (rule.required && component) {
148+
if (!Reflect.has(rule, 'type')) {
149+
rule.type = 'string';
150+
}
145151
const joinLabel = Reflect.has(props.schema, 'rulesMessageJoinLabel')
146152
? rulesMessageJoinLabel
147153
: globalRulesMessageJoinLabel;
@@ -157,11 +163,9 @@ export default defineComponent({
157163
component.includes('TimePicker')
158164
) {
159165
rule.type = 'object';
160-
}
161-
if (component.includes('RangePicker') || component.includes('Upload')) {
166+
} else if (component.includes('RangePicker') || component.includes('Upload')) {
162167
rule.type = 'array';
163-
}
164-
if (component.includes('InputNumber')) {
168+
} else if (component.includes('InputNumber')) {
165169
rule.type = 'number';
166170
}
167171
}
@@ -171,7 +175,7 @@ export default defineComponent({
171175
const characterInx = rules.findIndex((val) => val.max);
172176
if (characterInx !== -1 && !rules[characterInx].validator) {
173177
rules[characterInx].message =
174-
rules[characterInx].message || `字符数应小于${rules[characterInx].max}位`;
178+
rules[characterInx].message || t('maxTip', [rules[characterInx].max]);
175179
}
176180
return rules;
177181
}
@@ -237,6 +241,7 @@ export default defineComponent({
237241
const bindValue = {
238242
[valueField || (isCheck ? 'checked' : 'value')]: handleValue(component, field),
239243
};
244+
240245
if (!renderComponentContent) {
241246
return <Comp {...propsData} {...on} {...bindValue} />;
242247
}

src/components/Form/src/hooks/useFormAction.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ export function useFormAction({
7979
validKeys.push(key);
8080
}
8181
});
82-
// if (formEl) {
83-
// formEl.validateFields(validKeys);
84-
// }
82+
validateFields(validKeys);
8583
}
8684
/**
8785
* @description: Delete based on field name

src/components/Form/src/hooks/useFormValues.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export function useFormValues({
1919
formModel,
2020
}: UseFormValuesContext) {
2121
// Processing form values
22-
function handleFormValues(values: any) {
22+
function handleFormValues(values: Record<string, any>) {
2323
if (!isObject(values)) {
2424
return {};
2525
}
26-
const resMap: any = {};
26+
const resMap: Record<string, any> = {};
2727
for (const item of Object.entries(values)) {
2828
let [, value] = item;
2929
const [key] = item;
@@ -49,7 +49,7 @@ export function useFormValues({
4949
/**
5050
* @description: Processing time interval parameters
5151
*/
52-
function handleRangeTimeValue(values: any) {
52+
function handleRangeTimeValue(values: Record<string, any>) {
5353
const fieldMapToTime = unref(fieldMapToTimeRef);
5454

5555
if (!fieldMapToTime || !Array.isArray(fieldMapToTime)) {
@@ -72,7 +72,7 @@ export function useFormValues({
7272

7373
function initDefault() {
7474
const schemas = unref(getSchema);
75-
const obj: any = {};
75+
const obj: Record<string, any> = {};
7676
schemas.forEach((item) => {
7777
if (item.defaultValue) {
7878
obj[item.field] = item.defaultValue;

src/locales/lang/en/component/form.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export default {
66

77
input: 'Please Input',
88
choose: 'Please Choose',
9+
10+
maxTip: 'The number of characters should be less than {0}',
911
};

src/locales/lang/zh_CN/component/form.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export default {
66

77
input: '请输入',
88
choose: '请选择',
9+
10+
maxTip: '字符数应小于{0}位',
911
};

0 commit comments

Comments
 (0)