Control Panel configlets with JSONField

Hi!

I need a classic controlpanel entry to store configs for my addon in the registry (created with plonecli) for Classic UI. Can someone share a recipe to create a JSONField for a control panel configlet? For example, TinyMCE control panel uses JSON to store some configs but uses a Schema.Text and a validator.

Is this the way to go? I see some references to a JSONField somewhere but it is not clear how to use it.

plone.schema/jsonfield.py at master · plone/plone.schema · GitHub ?

Also: Google this site, I think similar questions have been asked before.

I'm testing JSONField with the schema. If you create a control panel configlet with plonecli, change from zope import schema to from plone import schema so you've some additional fields like JSONField.
If you don't put constraints in the schema, it always validate even if the json does not follow the schema. If the json is malformed, you get a traceback when saving (SyntaxError).

This is an example, if someone land in this page searching for info on JSONField:

CONF_SCHEMA = json.dumps(
    {
    "type": "object",
    "properties": {
        "vocab": {
        "description": "A list of items",
        "type": "array",
        "items": {
          "name": "string",
          "type": "string",
          "required": [ "name", "type" ]
        },
        "uniqueItems": True
        }
    }
    })

class MyControlPanel(Interface):
    biblioconfig = schema.JSONField(
        title=_(
            "My configurations",
        ),
        description=_(
            "",
        ),
        schema=CONF_SCHEMA,
        default={
        "vocab": [{"name": "banana",
                   "type": "fruit"},
                  {"sbapiid": "tomato",
                   "biblioid": "vegetable"}]
        },
        required=False,
        readonly=False,
    )

1 Like

I did this with a SourceText field like so (see validator above):

It store validated JSON as text, you need a JSON loads when using it.

I am not sure if this is the most perfect way, but works.

1 Like