Creating derived fields in json schema (version – draft -04)

04 / Feb / 2016 by Haider Ali 1 comments

Sometimes we need dynamic forms where some field’s visibility/values are dependent on values of other fields. Json schema provides a good option for creating dynamic forms but has very little documentation.

Json schema provides fairly coarse grained mechanism for conveying what elements needs to be present. Using anyOf condition, a property can be validated by a set of criteria that may include criteria that can match multiple times.

Using same you can create fields that are dependent on options you select.Let us take example to understand it.

[code]
{
"title": "Creating conditional fields",
"readOnly": false,
"$schema": "http://json-schema.org/draft-04/hyper-schema",
"description": "Creating conditional fields",
"properties": {
"form": {
"title": "Form",
"properties": {
"field": {
"title": "Select for field?",
"readOnly": false,
"options": {
"hidden": false
},
"strictProperties": true,

"entity": "field",
"propertyOrder": 1,
"type": "object",
"oneOf": [{
"title": "Please Select",
"properties": {

},
"additionalProperties": false
}, {
"title": "Yes",
"properties": {
"dependent": {
"title": "Dependent Field",
"readOnly": false,
"strictProperties": true,
"description": "",
"propertyOrder": 1,
"type": "string",
"options": {
"hidden": false
}
}
},
"additionalProperties": false,
"required": ["dependent"]
}, {
"title": "No",
"properties": {

},
"additionalProperties": false
}]
}
},
"propertyOrder": 1,
"type": "object",
"required": ["field"]
}
},
"type": "object"
}
[/code]

In above code we have form as main object, which contains one field object. Using anyOf we have created three sets of field. Those sets are identified as options will be displayed as titles. On the basis of what option/set you select dependent fields will be displayed.

Like if you select No :

No

Here no field is displayed as set is empty for No.

In case of Yes :

Yes

Here you can see the dependent field is displayed as set for Yes contains it.

The same way you can create multiple sets fields that are dependent on different options.

FOUND THIS USEFUL? SHARE IT

comments (1 “Creating derived fields in json schema (version – draft -04)”)

Leave a Reply

Your email address will not be published. Required fields are marked *