Commit bb1b267e2fc306608300ec09084b1f3d0cab7e59

Authored by vben
1 parent e04aaa06

fix(form): fix form verification and console error message issues

CHANGELOG.zh_CN.md
@@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
23 23
24 - 修复 tree 文本超出挡住操作按钮问题 24 - 修复 tree 文本超出挡住操作按钮问题
25 - 修复通过 useRedo 刷新页面参数丢失问题 25 - 修复通过 useRedo 刷新页面参数丢失问题
  26 +- 修复表单校验先设置在校验及控制台错误信息问题
26 27
27 ### 🎫 Chores 28 ### 🎫 Chores
28 29
src/components/Form/src/BasicForm.vue
@@ -71,17 +71,17 @@ @@ -71,17 +71,17 @@
71 const schemaRef = ref<Nullable<FormSchema[]>>(null); 71 const schemaRef = ref<Nullable<FormSchema[]>>(null);
72 const formElRef = ref<Nullable<FormActionType>>(null); 72 const formElRef = ref<Nullable<FormActionType>>(null);
73 73
74 - const getRowWrapStyleRef = computed((): any => {  
75 - const { baseRowStyle } = unref(getProps);  
76 - return baseRowStyle || {};  
77 - });  
78 -  
79 const getMergePropsRef = computed( 74 const getMergePropsRef = computed(
80 (): FormProps => { 75 (): FormProps => {
81 return deepMerge(cloneDeep(props), unref(propsRef)); 76 return deepMerge(cloneDeep(props), unref(propsRef));
82 } 77 }
83 ); 78 );
84 79
  80 + const getRowWrapStyleRef = computed((): any => {
  81 + const { baseRowStyle } = unref(getMergePropsRef);
  82 + return baseRowStyle || {};
  83 + });
  84 +
85 // 获取表单基本配置 85 // 获取表单基本配置
86 const getProps = computed( 86 const getProps = computed(
87 (): FormProps => { 87 (): FormProps => {
@@ -103,7 +103,7 @@ @@ -103,7 +103,7 @@
103 const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any); 103 const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any);
104 for (const schema of schemas) { 104 for (const schema of schemas) {
105 const { defaultValue, component } = schema; 105 const { defaultValue, component } = schema;
106 - if (defaultValue && dateItemType.includes(component!)) { 106 + if (defaultValue && dateItemType.includes(component)) {
107 if (!Array.isArray(defaultValue)) { 107 if (!Array.isArray(defaultValue)) {
108 schema.defaultValue = moment(defaultValue); 108 schema.defaultValue = moment(defaultValue);
109 } else { 109 } else {
src/components/Form/src/FormItem.tsx
@@ -17,6 +17,7 @@ import { upperFirst, cloneDeep } from &#39;lodash-es&#39;; @@ -17,6 +17,7 @@ import { upperFirst, cloneDeep } from &#39;lodash-es&#39;;
17 import { useItemLabelWidth } from './hooks/useLabelWidth'; 17 import { useItemLabelWidth } from './hooks/useLabelWidth';
18 import { ComponentType } from './types'; 18 import { ComponentType } from './types';
19 import { isNumber } from '/@/utils/is'; 19 import { isNumber } from '/@/utils/is';
  20 +import { useI18n } from '/@/hooks/web/useI18n';
20 21
21 export default defineComponent({ 22 export default defineComponent({
22 name: 'BasicFormItem', 23 name: 'BasicFormItem',
@@ -46,6 +47,8 @@ export default defineComponent({ @@ -46,6 +47,8 @@ export default defineComponent({
46 }, 47 },
47 }, 48 },
48 setup(props, { slots }) { 49 setup(props, { slots }) {
  50 + const { t } = useI18n('component.form');
  51 + // @ts-ignore
49 const itemLabelWidthRef = useItemLabelWidth(toRef(props, 'schema'), toRef(props, 'formProps')); 52 const itemLabelWidthRef = useItemLabelWidth(toRef(props, 'schema'), toRef(props, 'formProps'));
50 53
51 const getValuesRef = computed(() => { 54 const getValuesRef = computed(() => {
@@ -132,7 +135,7 @@ export default defineComponent({ @@ -132,7 +135,7 @@ export default defineComponent({
132 let rules: ValidationRule[] = cloneDeep(defRules) as ValidationRule[]; 135 let rules: ValidationRule[] = cloneDeep(defRules) as ValidationRule[];
133 136
134 if ((!rules || rules.length === 0) && required) { 137 if ((!rules || rules.length === 0) && required) {
135 - rules = [{ required }]; 138 + rules = [{ required, type: 'string' }];
136 } 139 }
137 140
138 const requiredRuleIndex: number = rules.findIndex( 141 const requiredRuleIndex: number = rules.findIndex(
@@ -142,6 +145,9 @@ export default defineComponent({ @@ -142,6 +145,9 @@ export default defineComponent({
142 if (requiredRuleIndex !== -1) { 145 if (requiredRuleIndex !== -1) {
143 const rule = rules[requiredRuleIndex]; 146 const rule = rules[requiredRuleIndex];
144 if (rule.required && component) { 147 if (rule.required && component) {
  148 + if (!Reflect.has(rule, 'type')) {
  149 + rule.type = 'string';
  150 + }
145 const joinLabel = Reflect.has(props.schema, 'rulesMessageJoinLabel') 151 const joinLabel = Reflect.has(props.schema, 'rulesMessageJoinLabel')
146 ? rulesMessageJoinLabel 152 ? rulesMessageJoinLabel
147 : globalRulesMessageJoinLabel; 153 : globalRulesMessageJoinLabel;
@@ -157,11 +163,9 @@ export default defineComponent({ @@ -157,11 +163,9 @@ export default defineComponent({
157 component.includes('TimePicker') 163 component.includes('TimePicker')
158 ) { 164 ) {
159 rule.type = 'object'; 165 rule.type = 'object';
160 - }  
161 - if (component.includes('RangePicker') || component.includes('Upload')) { 166 + } else if (component.includes('RangePicker') || component.includes('Upload')) {
162 rule.type = 'array'; 167 rule.type = 'array';
163 - }  
164 - if (component.includes('InputNumber')) { 168 + } else if (component.includes('InputNumber')) {
165 rule.type = 'number'; 169 rule.type = 'number';
166 } 170 }
167 } 171 }
@@ -171,7 +175,7 @@ export default defineComponent({ @@ -171,7 +175,7 @@ export default defineComponent({
171 const characterInx = rules.findIndex((val) => val.max); 175 const characterInx = rules.findIndex((val) => val.max);
172 if (characterInx !== -1 && !rules[characterInx].validator) { 176 if (characterInx !== -1 && !rules[characterInx].validator) {
173 rules[characterInx].message = 177 rules[characterInx].message =
174 - rules[characterInx].message || `字符数应小于${rules[characterInx].max}位`; 178 + rules[characterInx].message || t('maxTip', [rules[characterInx].max]);
175 } 179 }
176 return rules; 180 return rules;
177 } 181 }
@@ -237,6 +241,7 @@ export default defineComponent({ @@ -237,6 +241,7 @@ export default defineComponent({
237 const bindValue = { 241 const bindValue = {
238 [valueField || (isCheck ? 'checked' : 'value')]: handleValue(component, field), 242 [valueField || (isCheck ? 'checked' : 'value')]: handleValue(component, field),
239 }; 243 };
  244 +
240 if (!renderComponentContent) { 245 if (!renderComponentContent) {
241 return <Comp {...propsData} {...on} {...bindValue} />; 246 return <Comp {...propsData} {...on} {...bindValue} />;
242 } 247 }
src/components/Form/src/hooks/useFormAction.ts
@@ -79,9 +79,7 @@ export function useFormAction({ @@ -79,9 +79,7 @@ export function useFormAction({
79 validKeys.push(key); 79 validKeys.push(key);
80 } 80 }
81 }); 81 });
82 - // if (formEl) {  
83 - // formEl.validateFields(validKeys);  
84 - // } 82 + validateFields(validKeys);
85 } 83 }
86 /** 84 /**
87 * @description: Delete based on field name 85 * @description: Delete based on field name
src/components/Form/src/hooks/useFormValues.ts
@@ -19,11 +19,11 @@ export function useFormValues({ @@ -19,11 +19,11 @@ export function useFormValues({
19 formModel, 19 formModel,
20 }: UseFormValuesContext) { 20 }: UseFormValuesContext) {
21 // Processing form values 21 // Processing form values
22 - function handleFormValues(values: any) { 22 + function handleFormValues(values: Record<string, any>) {
23 if (!isObject(values)) { 23 if (!isObject(values)) {
24 return {}; 24 return {};
25 } 25 }
26 - const resMap: any = {}; 26 + const resMap: Record<string, any> = {};
27 for (const item of Object.entries(values)) { 27 for (const item of Object.entries(values)) {
28 let [, value] = item; 28 let [, value] = item;
29 const [key] = item; 29 const [key] = item;
@@ -49,7 +49,7 @@ export function useFormValues({ @@ -49,7 +49,7 @@ export function useFormValues({
49 /** 49 /**
50 * @description: Processing time interval parameters 50 * @description: Processing time interval parameters
51 */ 51 */
52 - function handleRangeTimeValue(values: any) { 52 + function handleRangeTimeValue(values: Record<string, any>) {
53 const fieldMapToTime = unref(fieldMapToTimeRef); 53 const fieldMapToTime = unref(fieldMapToTimeRef);
54 54
55 if (!fieldMapToTime || !Array.isArray(fieldMapToTime)) { 55 if (!fieldMapToTime || !Array.isArray(fieldMapToTime)) {
@@ -72,7 +72,7 @@ export function useFormValues({ @@ -72,7 +72,7 @@ export function useFormValues({
72 72
73 function initDefault() { 73 function initDefault() {
74 const schemas = unref(getSchema); 74 const schemas = unref(getSchema);
75 - const obj: any = {}; 75 + const obj: Record<string, any> = {};
76 schemas.forEach((item) => { 76 schemas.forEach((item) => {
77 if (item.defaultValue) { 77 if (item.defaultValue) {
78 obj[item.field] = item.defaultValue; 78 obj[item.field] = item.defaultValue;
src/locales/lang/en/component/form.ts
@@ -6,4 +6,6 @@ export default { @@ -6,4 +6,6 @@ export default {
6 6
7 input: 'Please Input', 7 input: 'Please Input',
8 choose: 'Please Choose', 8 choose: 'Please Choose',
  9 +
  10 + maxTip: 'The number of characters should be less than {0}',
9 }; 11 };
src/locales/lang/zh_CN/component/form.ts
@@ -6,4 +6,6 @@ export default { @@ -6,4 +6,6 @@ export default {
6 6
7 input: '请输入', 7 input: '请输入',
8 choose: '请选择', 8 choose: '请选择',
  9 +
  10 + maxTip: '字符数应小于{0}位',
9 }; 11 };