Skip to content

How to use dynamic schemas for multiple fields #22

Closed
@bernhardh

Description

@bernhardh

I am just wondering, how to add multiple fields based on one other field using the schemaResolver.

Lets say, I have a selectbox "type" with 2 options type1 and type2. Depending on this selectbox, I want to show 2 different sets of additional fields. I have tried the following:

var typeSchemas = {
    "type1": {
        "title": {
            "type": "string",
            "title": "Title 1"
        },
        "gender": {
            "type": "string",
            "title": "Gender",
            "enum": [
                "m",
                "f"
             ],
        },
    },
    "type2": {
        "title": {
            "type": "string",
            "title": "Title 2"
        },
        "otherfield": {
            "type": "string",
            "title": "Some other otherfield"
        },
    }
};
var myDiv = document.getElementById("#myDiv");
var bf = brutusin["json-forms"].create({
    "$schema": "http://json-schema.org/draft-03/schema#",
    "type": "object",
    "properties": properties = {
    "type": {
        "type": "string",
        "title": "Plugin-Typ",
        "description": "Choose the type of the next formfields",
        "required": true,
        "enum": [
            "type1",
            "type2"
        ]
    },
    "nextfields": {
        "dependsOn": [
            "type"
        ]
    }
}
});
bf.schemaResolver = function (names, data, cb) {
    var schemas = new Object();

    schemas["$.data"] = typeSchemas[data.type];
    setTimeout(function(){cb(schemas)},500);
};
bf.render(myDiv);

But thats not working, since the schemas["$.data"] has to be a single property and not an set of properties. Is there any possibility to solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions