feat(from): add required in schema

This commit is contained in:
vben 2020-11-05 22:53:47 +08:00
parent b6d5e5c96f
commit 2859067621
3 changed files with 13 additions and 5 deletions

View File

@ -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')
);

View File

@ -112,6 +112,8 @@ export interface FormSchema {
component: ComponentType;
// 组件参数
componentProps?: any;
// 必填
required?: boolean;
// 校验规则
rules?: RuleObject[];

View File

@ -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',