mirror of https://gitee.com/answerdev/answer.git
refactor(ui): check required field
This commit is contained in:
parent
bc904d8d59
commit
4bddea96da
|
@ -1154,3 +1154,4 @@ ui:
|
||||||
empty: cannot be empty
|
empty: cannot be empty
|
||||||
invalid: is invalid
|
invalid: is invalid
|
||||||
btn_submit: Save
|
btn_submit: Save
|
||||||
|
not_found_props: "Required property {{ key }} not found."
|
||||||
|
|
|
@ -85,7 +85,16 @@ const SchemaForm: FC<IProps> = ({
|
||||||
const { t } = useTranslation('translation', {
|
const { t } = useTranslation('translation', {
|
||||||
keyPrefix: 'form',
|
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 keys = Object.keys(properties);
|
||||||
|
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
@ -97,7 +106,6 @@ const SchemaForm: FC<IProps> = ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const requiredValidator = () => {
|
const requiredValidator = () => {
|
||||||
const required = schema.required || [];
|
|
||||||
const errors: string[] = [];
|
const errors: string[] = [];
|
||||||
required.forEach((key) => {
|
required.forEach((key) => {
|
||||||
if (!formData[key] || !formData[key].value) {
|
if (!formData[key] || !formData[key].value) {
|
||||||
|
@ -160,7 +168,7 @@ const SchemaForm: FC<IProps> = ({
|
||||||
isInvalid: true,
|
isInvalid: true,
|
||||||
errorMsg:
|
errorMsg:
|
||||||
uiSchema[cur]?.['ui:options']?.empty ||
|
uiSchema[cur]?.['ui:options']?.empty ||
|
||||||
`${schema.properties[cur].title} ${t('empty')}`,
|
`${schema.properties[cur]?.title} ${t('empty')}`,
|
||||||
};
|
};
|
||||||
return acc;
|
return acc;
|
||||||
}, formData);
|
}, formData);
|
||||||
|
|
Loading…
Reference in New Issue