brutusin / json-forms

JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
http://brutusin.org/json-forms
Apache License 2.0
607 stars 168 forks source link

Default values fix #63

Closed shadow-identity closed 7 years ago

shadow-identity commented 7 years ago

Hi, it's me again ;)

I've fixed bug when Default values doesn't appear in whole form if some part of Initial Data exists

Say, we have schema:

...
    "FOO": {
      "default": true,
      "type": "boolean"
    },
    "BAR": {
      "default": true,
      "type": "boolean"
    }
...

And if we haven't Initial Data at all, form will be rendered with those default values. But if we have, say, only {"BAR": true} in Initial Data, the default values for FOO will not be shown.

It's because getInitialValue(id) will return null in this case.

This PR fixes it.

idelvall commented 7 years ago

Hello! this has been deliberately implemented that way. The reason is to guarantee that form(initialData).getValue() = form(form(initialData).getValue()).getValue() , meaning that, using as initial data a value previously generated by the form, the new rendered form should be in the same state as the first one just before generating the data.

What do yo think? Why you need such behavior?

shadow-identity commented 7 years ago

Hello! In my case schema can be changed sometimes and user should see new default values of new schema for his existing config.

Why do you think my PR corrupts this behavior with form(initialData).getValue() = form(form(initialData).getValue()).getValue()?

idelvall commented 7 years ago

it does, in your case, given that the properties are not required, the user could unselect the BAR property and generate {"FOO":true}, and then use this value as initial state. With your PR, then automatically BAR will be selected to a true value, giving a immediate output of {"FOO":true, "BAR":true}.

If you give more details of your case maybe I can help you...

shadow-identity commented 7 years ago

OK, thanks, now I'm understand.