refactor(ui): check required field

This commit is contained in:
robin 2022-11-15 16:32:35 +08:00
parent bc904d8d59
commit 4bddea96da
2 changed files with 12 additions and 3 deletions

View File

@ -1154,3 +1154,4 @@ ui:
empty: cannot be empty
invalid: is invalid
btn_submit: Save
not_found_props: "Required property {{ key }} not found."

View File

@ -85,7 +85,16 @@ const SchemaForm: FC<IProps> = ({
const { t } = useTranslation('translation', {
keyPrefix: 'form',
});
const { properties } = schema;
const { required = [], properties } = schema;
// check required field
const excludes = required.filter((key) => !properties[key]);
if (excludes.length > 0) {
console.error(t('not_found_props', { key: excludes.join(', ') }));
}
const keys = Object.keys(properties);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@ -97,7 +106,6 @@ const SchemaForm: FC<IProps> = ({
};
const requiredValidator = () => {
const required = schema.required || [];
const errors: string[] = [];
required.forEach((key) => {
if (!formData[key] || !formData[key].value) {
@ -160,7 +168,7 @@ const SchemaForm: FC<IProps> = ({
isInvalid: true,
errorMsg:
uiSchema[cur]?.['ui:options']?.empty ||
`${schema.properties[cur].title} ${t('empty')}`,
`${schema.properties[cur]?.title} ${t('empty')}`,
};
return acc;
}, formData);