Skip to content

Commit 7279ea9

Browse files
revolunetn1k0
authored andcommitted
Fix rjsf-team#488: Add a custom Form ErrorList prop.
1 parent 7206a2a commit 7279ea9

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
5252
- [Advanced customization](#advanced-customization)
5353
- [Field template](#field-template)
5454
- [Array Field Template](#array-field-template)
55+
- [Error list Template](#error-list-template)
5556
- [Custom widgets and fields](#custom-widgets-and-fields)
5657
- [Custom widget components](#custom-widget-components)
5758
- [Custom component registration](#custom-component-registration)
@@ -809,6 +810,38 @@ The following props are part of each element in `items`:
809810
- `onReorderClick: (index, newIndex) => (event) => void`: Returns a function that swaps the items at `index` with `newIndex`.
810811
- `readonly`: A boolean value stating if the array item is readonly.
811812

813+
### Error List template
814+
815+
To take control over how the form errors are displayed, you can define an *error list template* for your form. This list is the form global error list that appears at the top of your forms.
816+
817+
An error list template is basically a React stateless component being passed errors as props so you can render them as you like:
818+
819+
```jsx
820+
function ErrorListTemplate(props) {
821+
const {errors} = props;
822+
return (
823+
<div>
824+
{errors.map((error, i) => {
825+
return (
826+
<li key={i}>
827+
{error.stack}
828+
</li>
829+
);
830+
})}
831+
</div>
832+
);
833+
}
834+
835+
render((
836+
<Form schema={schema}
837+
showErrorList={true}
838+
ErrorList={ErrorListTemplate} />,
839+
), document.getElementById("app"));
840+
```
841+
842+
> Note: Your custom `ErrorList` template will only render when `showErrorList` is `true`.
843+
844+
812845
### Custom widgets and fields
813846

814847
The API allows to specify your own custom *widget* and *field* components:
@@ -1256,6 +1289,8 @@ render((
12561289
), document.getElementById("app"));
12571290
```
12581291
1292+
> Note: you can also use your own [ErrorList](#error-list-template)
1293+
12591294
### The case of empty strings
12601295
12611296
When a text input is empty, the field in form data is set to `undefined`. String fields that use `enum` and a `select` widget work similarly and will have an empty option at the top of the options list that when selected will result in the field being `undefined`.

src/components/Form.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component, PropTypes } from "react";
22

3-
import ErrorList from "./ErrorList";
3+
import { default as DefaultErrorList } from "./ErrorList";
44
import {
55
getDefaultFormState,
66
shouldRender,
@@ -17,6 +17,7 @@ export default class Form extends Component {
1717
liveValidate: false,
1818
safeRenderCompletion: false,
1919
noHtml5Validate: false,
20+
ErrorList: DefaultErrorList,
2021
};
2122

2223
constructor(props) {
@@ -76,7 +77,7 @@ export default class Form extends Component {
7677

7778
renderErrors() {
7879
const { status, errors } = this.state;
79-
const { showErrorList } = this.props;
80+
const { ErrorList, showErrorList } = this.props;
8081
if (status !== "editing" && errors.length && showErrorList != false) {
8182
return <ErrorList errors={errors} />;
8283
}
@@ -208,6 +209,7 @@ if (process.env.NODE_ENV !== "production") {
208209
fields: PropTypes.objectOf(PropTypes.func),
209210
ArrayFieldTemplate: PropTypes.func,
210211
FieldTemplate: PropTypes.func,
212+
ErrorList: PropTypes.func,
211213
onChange: PropTypes.func,
212214
onError: PropTypes.func,
213215
showErrorList: PropTypes.bool,

test/validate_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from "react";
12
import { expect } from "chai";
23
import sinon from "sinon";
34
import { Simulate } from "react-addons-test-utils";
@@ -445,5 +446,32 @@ describe("Validation", () => {
445446
});
446447
});
447448
});
449+
450+
describe("Custom ErrorList", () => {
451+
const schema = {
452+
type: "string",
453+
required: true,
454+
minLength: 1,
455+
};
456+
457+
const formData = 0;
458+
459+
const CustomErrorList = ({ errors }) => (
460+
<div className="CustomErrorList">{errors.length} custom</div>
461+
);
462+
463+
it("should use CustomErrorList", () => {
464+
const { node } = createFormComponent({
465+
schema,
466+
liveValidate: true,
467+
formData,
468+
ErrorList: CustomErrorList,
469+
});
470+
expect(node.querySelectorAll(".CustomErrorList")).to.have.length.of(1);
471+
expect(node.querySelector(".CustomErrorList").textContent).eql(
472+
"1 custom"
473+
);
474+
});
475+
});
448476
});
449477
});

0 commit comments

Comments
 (0)