json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

Problem with empty choice in select drop downs

Open kshepard opened this issue 10 years ago • 4 comments

@jdorn: Thanks for merging #461 -- it's something that I also needed. I am noticing one problem though, and I'm not sure the best way to go about fixing it. When the empty value is selected, it removes the property from the object entirely. So for the demo on http://jeremydorn.com/json-editor/, if you select the empty option in the gender dropdown, the gender property will no longer show up in the json panel on the right side of the page. If you then click "Update Form", the gender dropdown will be removed from the form.

This seems like odd behavior to me. I think a common use-case for the json-editor is to be able to view an object, make changes, save, and be able to repeat that process as many times as needed. If someone selects the empty option in a dropdown, but then wants to later go back and make a selection, I believe the dropdown should still exist.

One simple (though possibly hacky) fix would be to switch the underlying empty value from undefined to an empty string. That would require changing those few undefined values in PR #461, and also modifying the enum validator to not mark empty strings as invalid. Though I think the real underlying problem may be the fact that when a property of an object does not exist, yet does exist in the schema, it is not currently rendered. This is the case with other types as well: for example, on the demo site, if I edit the json object and remove the name property, it is not rendered, even though it exists in the schema.

I'd appreciate your thoughts on this, and I'd be happy to help out with a solution.

@lepinay: Looping you in as well, as you may have given some thought to this.

kshepard avatar Aug 13 '15 22:08 kshepard

+1

n-irwin avatar Oct 14 '15 08:10 n-irwin

That's what the required attribute is for.

In this schema, the gender property is required and will always be in the resulting value. It can be either male, female, or an empty string:

{
  "type": "object",
  "required": ["gender"],
  "properties": {
    "gender": {
      "type": "string",
      "enum": ["","male","female"]
    }
  }
}

In this schema, the gender property is not required and will either be male, female, or absent from the resulting value:

{
  "type": "object",
  "properties": {
    "gender": {
      "type": "string",
      "enum": ["male","female"]
    }
  }
}

jdorn avatar Oct 14 '15 13:10 jdorn

I think not including it in the resulting value makes total sense. My problem is with what happens when you take that resulting value and load it back into the editor. If the gender is absent from the value in this case then the gender control is never rendered. You can see this in the demo if you select an empty gender value and then click "Update Form" -- the control will disappear. For my use case, users will be able to save a resulting value from the json-editor, and then they can go back and edit it as many times as they'd like. But with the current setup, if they ever select an empty value, that control will never be rendered again, and therefore they'll never be able to change it to a non-null value.

kshepard avatar Oct 14 '15 18:10 kshepard

Is there any solution for this? I want to be able set remove_empty_properties=true and then when usere reload form it should be able edit this empty field. I found similar issues #107, #755 Also #153 marked as fixed by #173 but I can not understend how.

editor.root.setValue(value, true)

seems works but it fill missed values to default vaules.

moteus avatar Sep 07 '17 12:09 moteus