Skip to content

Commit 6e9079b

Browse files
garykwn1k0
authored andcommitted
Introduce the showErrorList prop for hiding the top error list (rjsf-team#269)
1 parent 5112f48 commit 6e9079b

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

README.md

Lines changed: 22 additions & 1 deletion
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
- [Form data validation](#form-data-validation)
5353
- [Live validation](#live-validation)
5454
- [Custom validation](#custom-validation)
55+
- [Error List Display](#error-list-display)
5556
- [Styling your forms](#styling-your-forms)
5657
- [Schema definitions and references](#schema-definitions-and-references)
5758
- [JSON Schema supporting status](#json-schema-supporting-status)
@@ -709,7 +710,6 @@ The `registry` is an object containing the registered custom fields and widgets
709710
- `definitions`: The root schema [definitions](#schema-definitions-and-references), if any.
710711

711712
The registry is passed down the component tree, so you can access it from your custom field and `SchemaField` components.
712-
713713
### Custom SchemaField
714714

715715
**Warning:** This is a powerful feature as you can override the whole form behavior and easily mess it up. Handle with care.
@@ -837,6 +837,27 @@ render(<Form schema={schema} validate={validate} />);
837837
> received as second argument.
838838
> - The `validate()` function is called **after** the JSON schema validation.
839839
840+
### Error List Display
841+
842+
Form data allows you to hide the display of the validation Header.
843+
844+
To disable the Error List display, you can set Form's showErrorList prop to false. Doing so will still validate the form but only the inline display will show.
845+
846+
```js
847+
const schema = {
848+
type: "object",
849+
required: ["foo"],
850+
properties: {
851+
foo: {type: "string"},
852+
bar: {type: "string"},
853+
}
854+
};
855+
856+
render(<Form schema={schema} showErrorList={false}/>);
857+
858+
```
859+
860+
840861
841862
## Styling your forms
842863

src/components/Form.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ export default class Form extends Component {
6969

7070
renderErrors() {
7171
const {status, errors} = this.state;
72-
if (status !== "editing" && errors.length) {
72+
const {showErrorList} = this.props;
73+
74+
if (status !== "editing" && errors.length && showErrorList != false) {
7375
return <ErrorList errors={errors} />;
7476
}
7577
return null;
@@ -193,6 +195,7 @@ if (process.env.NODE_ENV !== "production") {
193195
fields: PropTypes.objectOf(PropTypes.func),
194196
onChange: PropTypes.func,
195197
onError: PropTypes.func,
198+
showErrorList: PropTypes.bool,
196199
onSubmit: PropTypes.func,
197200
id: PropTypes.string,
198201
className: PropTypes.string,

test/validate_test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,5 +330,51 @@ describe("Validation", () => {
330330
});
331331
});
332332
});
333+
334+
describe("showErrorList prop validation", () => {
335+
describe("Required fields", () => {
336+
const schema = {
337+
type: "object",
338+
required: ["foo"],
339+
properties: {
340+
foo: {type: "string"},
341+
bar: {type: "string"},
342+
}
343+
};
344+
345+
var comp, node, onError;
346+
347+
beforeEach(() => {
348+
onError = sandbox.spy();
349+
const compInfo = createFormComponent({schema, formData: {
350+
foo: undefined
351+
}, onError, showErrorList: false});
352+
comp = compInfo.comp;
353+
node = compInfo.node;
354+
355+
Simulate.submit(node);
356+
});
357+
358+
it("should validate a required field", () => {
359+
expect(comp.state.errors)
360+
.to.have.length.of(1);
361+
expect(comp.state.errors[0].message)
362+
.eql(`requires property "foo"`);
363+
});
364+
365+
it("should not render error list if showErrorList prop true", () => {
366+
expect(node.querySelectorAll(".errors li"))
367+
.to.have.length.of(0);
368+
});
369+
370+
it("should trigger the onError handler", () => {
371+
sinon.assert.calledWith(onError, sinon.match(errors => {
372+
return errors[0].message === `requires property "foo"`;
373+
}));
374+
});
375+
});
376+
377+
});
378+
333379
});
334380
});

0 commit comments

Comments
 (0)