Skip to content

Commit 14993f5

Browse files
committed
Update docs for next version
1 parent 385d655 commit 14993f5

File tree

8 files changed

+414
-37
lines changed

8 files changed

+414
-37
lines changed

docs/docs/schema.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ Also, checkout the [playground]({{ '/playground' | url }}).
2323

2424
Documented here: <https://django-jsonform.readthedocs.io/en/latest/guide/upload.html>
2525

26+
Although the code examples in the document is for Django (Python) backend, but
27+
the concept can be adapted to any language.
28+
29+
## Autocomplete input
30+
31+
Documented here: <https://django-jsonform.readthedocs.io/en/latest/guide/autocomplete.html>
32+
2633
Although the code examples in the document is for Django (Python) backend, but
2734
the concept can be adapted to any language.

docs/docs/usage/browser.md

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You'll also need to have a `textarea` where the form will save the data.
2525
<script type="text/javascript">
2626
var form = reactJsonForm.createForm({
2727
containerId: 'formContainer',
28-
containerId: 'formData',
28+
dataInputId: 'formData',
2929
schema: {
3030
type: 'object',
3131
keys: {
@@ -51,6 +51,39 @@ form.addEventListener('change', function(e) {
5151
See [`addEventListener()`](#forminstance.addeventlistener(event%2C-callback)) section for
5252
further details about handling events.
5353

54+
55+
## Data validation
56+
57+
*New in version 2.1*
58+
59+
The form component provides basic data validation.
60+
61+
Usage example:
62+
63+
```js
64+
var validation = form.validate();
65+
66+
var isValid = validation.isValid; // it will be false if data is not valid
67+
68+
var errorMap = validation.errorMap; // object containing error messages
69+
70+
if (!isValid) {
71+
// notify user
72+
alert('Please correct the errors');
73+
74+
// update the form component
75+
// it will display error messages below each input
76+
form.update({errorMap: errorMap});
77+
}
78+
79+
```
80+
81+
You can adopt the above code example to validate the data before a form is submitted.
82+
83+
You can also implement custom validation instead of calling `.validate()`. In that
84+
case, you'll have to manually create an [`errorMap` object]({{ '/docs/usage/node/#data-validation' | url }})
85+
for displaying error messages.
86+
5487
## API reference
5588

5689
### Library functions
@@ -65,6 +98,9 @@ which may contain these keys:
6598
- `schema`: The schema of the form.
6699
- `data` *(Optional)*: The initial data of the form.
67100
- `fileHandler` *(Optional)*: URL for the common file handler endpoint for all file fields.
101+
- `errorMap` *(Optional)*: An object containing error messages for fields.
102+
103+
*Changed in version 2.1*: `errorMap` option was added.
68104

69105

70106
##### `reactJsonForm.getFormInstance(containerId)`
@@ -90,26 +126,54 @@ The following methods, attributes & events are available on a form instance.
90126

91127
Register a callback for a given event ([see available events](#events)).
92128

93-
The callback function will be passed an `Object` with the following keys:
129+
##### `formInstance.render()`
130+
131+
Function to render the form.
132+
133+
##### `formInstance.update(config)`
134+
135+
Re-render the form with the given `config`.
136+
137+
138+
##### `formInstance.validate()`
139+
140+
*New in version 2.1*
141+
142+
Validates the current data against the instance's schema.
143+
144+
Returns a *validation* object with following keys:
145+
146+
- `isValid`: It will be `true` if data is valid, else `false`.
147+
- `errorMap`: An object containing field names and validation errors.
148+
149+
##### `formInstance.getData()`
150+
151+
*New in version 2.1*
152+
153+
Returns the current data of the form instance.
154+
155+
##### `formInstance.getSchema()`
156+
157+
*New in version 2.1*
158+
159+
Returns the current schema of the form instance.
160+
161+
162+
#### Events
163+
164+
Following is the list of currently available events:
165+
166+
##### `change`
167+
168+
This event is fired for every change in the form's data.
169+
170+
The callback for this event will be passed an `Object` with the following keys:
94171

95172
- `data`: Current data of the form.
96173
- `prevData`: Previous data of the form (before the event).
97174
- `schema`: Current schema of the form.
98175
- `prevSchema`: Previous schema of the form (before the event).
99176

100-
<div class="alert alert-info">
101-
<p><strong>Attention!</strong></p>
102-
<p>
103-
If you want to call the <a href="#forminstance.update(config)"><code>update()</code></a>
104-
method from within an event listener, <strong>you must call it conditionally</strong>
105-
or else it might cause an infite loop.
106-
</p>
107-
<p>
108-
For example, only call the <code>update()</code> after checking that the
109-
current <code>data</code> and <code>prevData</code> are not same.
110-
</p>
111-
</div>
112-
113177
Example:
114178

115179
```js
@@ -125,17 +189,15 @@ form.addEventListener('change', function(e) {
125189
});
126190
```
127191

128-
##### `formInstance.render()`
129-
130-
Function to render the form.
131-
132-
##### `formInstance.update(config)`
133-
134-
Re-render the form with the given `config`.
135-
136-
137-
#### Events
138-
139-
Following is the list of currently available events:
140-
141-
- `change`
192+
<div class="alert alert-info">
193+
<p><strong>Attention!</strong></p>
194+
<p>
195+
If you want to call the <a href="#forminstance.update(config)"><code>update()</code></a>
196+
method from the <code>change</code> event listener, <strong>you must call it conditionally</strong>
197+
or else it might cause an infite loop.
198+
</p>
199+
<p>
200+
For example, only call the <code>update()</code> after checking that the
201+
current <code>data</code> and <code>prevData</code> are not same.
202+
</p>
203+
</div>

docs/docs/usage/node.md

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,15 @@ class MyComponent extends React.Component {
6464

6565
### Props
6666

67-
- `editorState`: Instance of [`EditorState`](#editorstate) containing the schema and data.
68-
- `onChange`: Callback function for handling changing. This function will receive a new instance of
67+
- `editorState`: Instance of [`EditorState`](#editorstate-api-reference) containing the schema and data.
68+
- `onChange`: Callback function for handling changes. This function will receive a new instance of
6969
`EditorState` (because `EditorState` is immutable, instead of modifying the previous instance, we
7070
replace it with a new one).
7171
- `fileHandler`: A URL to a common file handler endpoint for all file input fields.
72+
- `errorMap`: An object containing error messages for input fields. [See data validation section](#data-validation)
73+
for more.
74+
75+
*Changed in version 2.1*: `errorMap` prop was added.
7276

7377
## `EditorState` API reference
7478

@@ -132,3 +136,89 @@ declared in the schema.
132136
##### `EditorState.getSchema()`
133137

134138
This method returns the schema.
139+
140+
141+
#### Data validation
142+
143+
*New in version 2.1*
144+
145+
**React JSON Form** comes with some basic data validator called [`DataValidator`](#datavalidator-api-reference).
146+
But you are free to validate the data however you want.
147+
148+
After the validation, you may also want to display error messages below the
149+
input fields. For this purpose, the `ReactJSONForm` component accepts an `errorMap`
150+
prop which is basically a mapping of field names in the data and error messages.
151+
152+
An `errorMap` looks like this:
153+
154+
```js
155+
let errorMap = {
156+
'name': 'This field is required',
157+
158+
// multiple error messages
159+
'age': [
160+
'This field is required',
161+
'This value must be greater than 18'
162+
]
163+
164+
// nested arrays and objects
165+
166+
// first item in array
167+
'0': 'This is required',
168+
169+
// first item > object > property: name
170+
'0-name': 'This is required'
171+
}
172+
```
173+
174+
##### `DataValidator` API reference
175+
176+
##### Constructor
177+
178+
##### `new DataValidator(schema)`
179+
180+
**Returns**: An instance of `DataValidator`
181+
182+
**Arguments**:
183+
184+
- `schema`: Schema object (not JSON string).
185+
186+
##### Instance methods
187+
188+
Following methods must be called form the instance of `DataValidator`.
189+
190+
##### `validatorInstance.validate(data)`
191+
192+
**Returns**: A *validation* object containing these keys:
193+
194+
- `isValid`: A boolean denoting whether the data is valid or not.
195+
- `errorMap`: An object containing error messages for invalid data fields.
196+
197+
**Arguments**:
198+
199+
- `data`: The data to validate against the `schema` provided to the constructor.
200+
201+
Example:
202+
203+
```jsx
204+
import {DataValidator} from '@bhch/react-json-form';
205+
206+
const validator = new DataValidator(schema);
207+
const validation = validator.validate(data);
208+
209+
const isValid = validation.isValid;
210+
const errorMap = validation.errorMap;
211+
212+
if (isValid)
213+
alert('Success');
214+
else
215+
alert('Invalid');
216+
217+
// pass the errorMap object to ReactJSONForm
218+
// and error messages will be displayed under
219+
// input fields
220+
<ReactJSONForm
221+
editorState={...}
222+
errorMap={errorMap}
223+
/>
224+
```

docs/src/demos.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ const DEMOS = [
225225
}
226226
},
227227

228+
{
229+
name: 'Autocomplete',
230+
slug: 'autocomplete',
231+
schema: {
232+
type: 'object',
233+
keys: {
234+
country: {type: 'string', widget: 'autocomplete', handler: '/'},
235+
}
236+
},
237+
description: () => (
238+
<div>
239+
Autocomplete widget sends AJAX request to a server. Hence, this demo will
240+
not show any options because there's no server.
241+
</div>
242+
)
243+
},
244+
228245
{
229246
name: 'Textarea',
230247
slug: 'textarea',
@@ -237,6 +254,18 @@ const DEMOS = [
237254
}
238255
},
239256

257+
{
258+
name: 'Range input',
259+
slug: 'range',
260+
schema: {
261+
type: 'object',
262+
title: 'Range input',
263+
properties: {
264+
volume: {type: 'number', widget: 'range', minimum: 0, maximum: 10}
265+
}
266+
}
267+
},
268+
240269
{
241270
name: 'Placeholder & Help text',
242271
slug: 'placehlder-help-text',
@@ -276,10 +305,22 @@ const DEMOS = [
276305
keys: {
277306
email: {type: 'string', format: 'email'},
278307
password: {type: 'string', format: 'password'},
279-
range: {type: 'string', format: 'range'},
280308
colour: {type: 'string', format: 'color'},
281309
}
282310
}
311+
},
312+
313+
{
314+
name: 'Validation',
315+
slug: 'validation',
316+
schema: {
317+
type: 'object',
318+
title: 'Press "Submit" to validate data',
319+
keys: {
320+
name: {type: 'string', required: true},
321+
age: {type: 'number', required: true, minimum: 50},
322+
}
323+
}
283324
}
284325
];
285326

0 commit comments

Comments
 (0)