Skip to content

Commit 1eb5b73

Browse files
olzraitin1k0
authored andcommitted
Pass more props to ErrorList (rjsf-team#658)
The following props are now passed to `ErrorList` - `errors`: An array of the errors. - `errorSchema`: The errorSchema constructed by `Form`. - `schema`: The schema that was passed to `Form`. - `uiSchema`: The uiSchema that was passed to `Form`. - `formContext`: The `formContext` object that you passed to Form.
1 parent b3d03fa commit 1eb5b73

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,13 @@ render((
896896

897897
> Note: Your custom `ErrorList` template will only render when `showErrorList` is `true`.
898898
899+
The following props are passed to `ErrorList`
900+
901+
- `errors`: An array of the errors.
902+
- `errorSchema`: The errorSchema constructed by `Form`.
903+
- `schema`: The schema that was passed to `Form`.
904+
- `uiSchema`: The uiSchema that was passed to `Form`.
905+
- `formContext`: The `formContext` object that you passed to Form.
899906

900907
### Custom widgets and fields
901908

src/components/Form.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,19 @@ export default class Form extends Component {
7777
}
7878

7979
renderErrors() {
80-
const { status, errors } = this.state;
81-
const { ErrorList, showErrorList } = this.props;
80+
const { status, errors, errorSchema, schema, uiSchema } = this.state;
81+
const { ErrorList, showErrorList, formContext } = this.props;
8282

8383
if (status !== "editing" && errors.length && showErrorList != false) {
84-
return <ErrorList errors={errors} />;
84+
return (
85+
<ErrorList
86+
errors={errors}
87+
errorSchema={errorSchema}
88+
schema={schema}
89+
uiSchema={uiSchema}
90+
formContext={formContext}
91+
/>
92+
);
8593
}
8694
return null;
8795
}

test/validate_test.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,24 +513,57 @@ describe("Validation", () => {
513513
minLength: 1,
514514
};
515515

516+
const uiSchema = {
517+
foo: "bar",
518+
};
519+
516520
const formData = 0;
517521

518-
const CustomErrorList = ({ errors }) =>
519-
<div className="CustomErrorList">
520-
{errors.length} custom
522+
const CustomErrorList = ({
523+
errors,
524+
errorSchema,
525+
schema,
526+
uiSchema,
527+
formContext: { className },
528+
}) =>
529+
<div>
530+
<div className="CustomErrorList">
531+
{errors.length} custom
532+
</div>
533+
<div className={"ErrorSchema"}>
534+
{errorSchema.__errors[0]}
535+
</div>
536+
<div className={"Schema"}>
537+
{schema.type}
538+
</div>
539+
<div className={"UiSchema"}>
540+
{uiSchema.foo}
541+
</div>
542+
<div className={className} />
521543
</div>;
522544

523545
it("should use CustomErrorList", () => {
524546
const { node } = createFormComponent({
525547
schema,
548+
uiSchema,
526549
liveValidate: true,
527550
formData,
528551
ErrorList: CustomErrorList,
552+
formContext: { className: "foo" },
529553
});
530554
expect(node.querySelectorAll(".CustomErrorList")).to.have.length.of(1);
531555
expect(node.querySelector(".CustomErrorList").textContent).eql(
532556
"1 custom"
533557
);
558+
expect(node.querySelectorAll(".ErrorSchema")).to.have.length.of(1);
559+
expect(node.querySelector(".ErrorSchema").textContent).eql(
560+
"is required"
561+
);
562+
expect(node.querySelectorAll(".Schema")).to.have.length.of(1);
563+
expect(node.querySelector(".Schema").textContent).eql("string");
564+
expect(node.querySelectorAll(".UiSchema")).to.have.length.of(1);
565+
expect(node.querySelector(".UiSchema").textContent).eql("bar");
566+
expect(node.querySelectorAll(".foo")).to.have.length.of(1);
534567
});
535568
});
536569
});

0 commit comments

Comments
 (0)