rjsf-errors-on-array-template (#1683)

* display error messages on the ArrayFieldTemplate for item list errors

* do not change things that do not need it

---------

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
jasquat 2024-06-05 14:31:59 -04:00 committed by GitHub
parent 4611628554
commit 9150bb74b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import {
RJSFSchema,
StrictRJSFSchema,
} from '@rjsf/utils';
import { getCommonAttributes } from '../../helpers';
/** The `ArrayFieldTemplate` component is the template used to render all items in an array.
*
@ -31,6 +32,7 @@ export default function ArrayFieldTemplate<
required,
schema,
title,
rawErrors,
} = props;
const uiOptions = getUiOptions<T, S, F>(uiSchema);
const ArrayFieldDescriptionTemplate = getTemplate<
@ -50,10 +52,14 @@ export default function ArrayFieldTemplate<
S,
F
>('ArrayFieldTitleTemplate', registry, uiOptions);
const commonAttributes = getCommonAttributes('', schema, uiSchema, rawErrors);
// Button templates are not overridden in the uiSchema
const {
ButtonTemplates: { AddButton },
} = registry.templates;
return (
<fieldset className={className} id={idSchema.$id}>
<ArrayFieldTitleTemplate
@ -71,6 +77,14 @@ export default function ArrayFieldTemplate<
uiSchema={uiSchema}
registry={registry}
/>
{commonAttributes.errorMessageForField && (
<>
<div className="error-message">
{commonAttributes.errorMessageForField}
</div>
<br />
</>
)}
<div className="row array-item-list">
{items &&
items.map(

View File

@ -37,7 +37,7 @@ export const getCommonAttributes = (
errorMessageForField = (schema as any).validationErrorMessage;
errorMessageForFieldWithoutLabel = errorMessageForField;
} else {
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`;
errorMessageForField = `"${labelToUse.replace(/\*$/, '')}" ${rawErrors[0]}`;
}
}