menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

JSONForms

JSON Forms is a JSON Schema based approach for creating forms and comes with support for React, Angular and Vue. Support here is free, but might involve some delay in the replies based on our current workload for paying customers. We also offer PROFESSIONAL SUPPORT packages to get you started quickly with JSON Forms and to help you with any questions and technical difficulties for JSON Forms with guaranteed response time and in-depth technical solutions. For details please see our web page linked below.

Channels
Team

Help with default values

April 9, 2020 at 6:37pm

Help with default values

April 9, 2020 at 6:37pm
Hello!
I'm on 2.3.2 and can't seem to get default values to appear. I notice that there are default values set in schema in this example but it is unused https://jsonforms.io/examples/layouts.
Upon loading, I'm met with empty fields that are not populated with default values,
Here is part of my schema written in yaml.
nationality:
default: US
enum:
- DE
- IT
- INDO
- JP
- US
- RU
type: string
number_field:
default: 6
maximum: 10
minimum: 5
type: number
rico_field:
type: string
since_sync:
type: string
test_field:
default: default value
type: string
And my component
<JsonForms
data={data}
schema={schema}
uischema={uischema}
renderers={materialRenderers}
cells={materialCells}
onChange={({ errors, data }) => {console.log(data); updateData(errors, data)}}
/>

April 15, 2020 at 6:47am
Hi , we let Ajv handle the default values. As we require certain Ajv settings you can use the exported Ajv factory createAjv(options) and set useDefaults: true. The customized Ajv can then be passed into the JsonForms component, e.g.
const handleDefaultsAjv = createAjv({useDefaults: true});
// [...]
<JsonForms
data={data}
schema={schema}
uischema={uischema}
renderers={materialRenderers}
cells={materialCells}
onChange={({ errors, data }) => {console.log(data); updateData(errors, data)}}
ajv={handleDefaultsAjv}
/>
like-fill
1