diff --git a/src/components/Form/src/FormItem.tsx b/src/components/Form/src/FormItem.tsx index ad6bfc32..f36099a9 100644 --- a/src/components/Form/src/FormItem.tsx +++ b/src/components/Form/src/FormItem.tsx @@ -102,13 +102,19 @@ export default defineComponent({ rulesMessageJoinLabel, label, dynamicRules, + required, } = props.schema; if (isFunction(dynamicRules)) { return dynamicRules(unref(getValuesRef)); } - const rules: ValidationRule[] = cloneDeep(defRules); + let rules: ValidationRule[] = cloneDeep(defRules); + + if ((!rules || rules.length === 0) && required) { + rules = [{ required }]; + } + const requiredRuleIndex: number = rules.findIndex( (rule) => Reflect.has(rule, 'required') && !Reflect.has(rule, 'validator') ); diff --git a/src/components/Form/src/types/form.ts b/src/components/Form/src/types/form.ts index 1ad73d4f..0b3a371c 100644 --- a/src/components/Form/src/types/form.ts +++ b/src/components/Form/src/types/form.ts @@ -112,6 +112,8 @@ export interface FormSchema { component: ComponentType; // 组件参数 componentProps?: any; + // 必填 + required?: boolean; // 校验规则 rules?: RuleObject[]; diff --git a/src/views/demo/form/RuleForm.vue b/src/views/demo/form/RuleForm.vue index b82e56a4..728e0fcf 100644 --- a/src/views/demo/form/RuleForm.vue +++ b/src/views/demo/form/RuleForm.vue @@ -24,7 +24,7 @@ colProps: { span: 8, }, - rules: [{ required: true }], + required: true, }, { field: 'field2', @@ -33,7 +33,7 @@ colProps: { span: 8, }, - rules: [{ required: true }], + required: true, }, { field: 'field3', @@ -42,7 +42,7 @@ colProps: { span: 8, }, - rules: [{ required: true }], + required: true, }, { field: 'field4', @@ -65,7 +65,7 @@ }, ], }, - rules: [{ required: true }], + rules: [{ required: true, message: '请输入aa' }], }, { field: 'field5',