Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define defaults for an array #158

Open
captain828 opened this issue Feb 28, 2024 · 3 comments
Open

Define defaults for an array #158

captain828 opened this issue Feb 28, 2024 · 3 comments

Comments

@captain828
Copy link

Is there a way to define defaults for an array?

{
    'type': 'array',
    'items': {
        'type': 'string',
        'choices': ['Eggs', 'Milk', 'Juice'],
        'widget': 'multiselect'
    }
}

I've tried:

{
    'type': 'array',
    'items': {
        'type': 'string',
        'choices': ['Eggs', 'Milk', 'Juice'],
        'widget': 'multiselect'
    },
   'default': ['Eggs', 'Milk']
}

but it's not working.

@bhch
Copy link
Owner

bhch commented Feb 29, 2024

I'm unable to reproduce this. It's working fine in the playground.

You can also test your schema in the playground (be sure to make it valid json i.e. use double quotes instead of single).

Also, which version are you using. Find out using this command:

$ python -c "import django_jsonform; print(django_jsonform.__version__)"

@captain828
Copy link
Author

Hhhm, it does work on the playground and also if I give it a schema on the model field but not via Javascript, using

form.update({
            schema: schema,
            data: form.getData()
        });

The actual schema I am sending is:

{
    "default": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
    "items": {
	    "enum": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
	    "type": "string",
	    "widget": "multiselect"
    },
    "type": "array"
}

(generated from a Pydantic model)

All other defaults are working fine.

@bhch
Copy link
Owner

bhch commented Mar 1, 2024

This might be a bug or it might be the expected behaviour.

I don't think the form will populate the defaults on updates.

E.g. let's say a user has de-selected the defaults from the input, and so later on when update() is called, the form should keep the array empty instead of re-populating the defaults because the user had already edited the input and removed them.

Though I'm not exactly sure if this is what's really going on.

If you could provide me exact code to reproduce this, I will be conclusively able to tell whether it is a bug or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants