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