django-jsonform icon indicating copy to clipboard operation
django-jsonform copied to clipboard

Choice field returning empty values

Open kayesn786 opened this issue 2 years ago • 6 comments

Hi @bhch

I have a form displaying the following address information. The state choices is filled based on country through jQuery. The problem is, the address object in DB is empty i.e. {country:"",state:""}.

Am i missing something?

django version: 3.2 using jazzmin for the dashboard UI

ADDRESS={ "type": "dict", "keys": { "country": { "type": "string", 'choices': ['India','China',....] }, "state": { "type": "string", 'choices': [] } } }

//models.js address = JSONField(schema=ADDRESS)

kayesn786 avatar Jul 25 '22 13:07 kayesn786

Are you populating the state input field using JQuery?

bhch avatar Jul 25 '22 13:07 bhch

Yes! But I tried with static values in state too. It is still same as using jQuery

kayesn786 avatar Jul 25 '22 13:07 kayesn786

The actual JSON data is kept in a hidden textarea. So modifying the input field with jQuery will not change the data.

But it's strange that it's not working with predefined values either. Could you tell me which version you're using?

Check it with this command:

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

bhch avatar Jul 25 '22 15:07 bhch

version: 2.10.1 At least the country field is predefined. So am expecting a value for that.

If it is a normal schema with array of objects or so, I can see the data in clean method (populated through jQuery)

kayesn786 avatar Jul 25 '22 15:07 kayesn786

I tried the schema you gave me but I'm unable to reproduce this issue. It's working fine for me.

If you could create a sample repository with some code to reproduce this, it'll be quicker for me to just clone and test it out.

bhch avatar Jul 25 '22 16:07 bhch

@kayesn786 Do you still have this issue?

Currently, I'm working on a new version. I've been thinking about providing some JavaScript apis to control the form in the browser, such as events handlers and functions etc. Those will be useful in cases like this issue.

If you have any ideas, I'd appreciate your input.

bhch avatar Aug 03 '22 20:08 bhch

@bhch the issue was with adding dynamic options with jQuery. Django was not recognizing those dynamically added . The solution was to pass all possible options while form is rendered and hide then on document load.

However, I would love to have a feature that selects a schema based on some other field in the form.

kayesn786 avatar Aug 05 '22 04:08 kayesn786

Hi, I've added a basic JavaScript API (see docs) which will allow you to dynamically update the schema and the widget.

Please upgrade to v2.11.0 and test it out.


In future, I'll add support for if-then-else keywords in the schema.

bhch avatar Aug 16 '22 16:08 bhch