abogushov / django-admin-json-editor

Adds json-editor for JSONField in Django Administration
MIT License
173 stars 61 forks source link

how to set required fields ? #3

Closed AdrienLemaire closed 6 years ago

AdrienLemaire commented 6 years ago

It looks like json-editor allows setting a required option to define what fields should be required or not.

I tried a schema like this:

HOST_ROLES_SCHEMA = {
    'type': 'array',
    'title': 'roles',
    'format': 'table',
    'items': {
        'type': 'object',
        'required': [
            'name',
            'tag',
        ],
        'properties': {
            'name': {
                'title': 'Name',
                'type': 'string',
                'format': 'text',
            },
            'tag': {
                'title': 'Tag',
                'type': 'string',
                'format': 'text',
            }
         }
    }
}

I also tried this version: I tried a schema like this:

HOST_ROLES_SCHEMA = {
    'type': 'array',
    'title': 'roles',
    'format': 'table',
    'items': {
        'type': 'object',
        'properties': {
            'name': {
                'title': 'Name',
                'type': 'string',
                'format': 'text',
                'required': True,
            },
            'tag': {
                'title': 'Tag',
                'type': 'string',
                'format': 'text',
                'required': True,
            }
         }
    }
}

But I can keep saving the model with empty fields in django admin. Is this property not supported ? Do I need to add some custom code to handle it myself ? Not seeing any required html attribute or class name in the generated html output, I'm not sure where to go from there.

abogushov commented 6 years ago

@Fandekasp I have added an example with required fields.

abogushov commented 6 years ago

The required allows you to specify the keys that must be presented in the object and values can be empty. If you want a non-empty value you should use minLength property.

AdrienLemaire commented 6 years ago

Sorry for the late reply. Thanks !