question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Display form inputs for unpopulated properties

See original GitHub issue

General information

  • json-editor version: latest

Expected behavior

I’m not sure if the library is supposed to be able to do this, but here is what I am trying to do:

  1. Use jsonschema to describe a form with many fields
  2. Seed the form using startval with values some (but not all) fields
  3. Display the entire form, including all empty fields

Here is the code that I’ve tried:

<div id="holder"></div>
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<script>
var schema = {
    "foo": {
        "type": "object",
        "title": "Foo",
        "properties": {
            "bar": {
                "type": "string",
                "title": "Bar"
            },
            "baz": {
                "type": "string",
                "title": "Baz"
            }
        }
    }
};
var settings = {
    "foo": {
        "bar": "Hello world"
    }
};
var editor = new JSONEditor(document.getElementById('holder'), {
   schema: schema,
   startval: settings
});

Actual behavior

I was hoping to see “Bar” filled in with “Hello world” and “Baz” as an empty text input. But so far I am only seeing “Bar” filled in with “Hello world”. The “Baz” field does not appear.

Any advice would be greatly appreciated, if there is an easy way to get what I’m looking for. Thank you!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kameelyancommented, Nov 2, 2020

Hey @brockfanning, it’s a common misconception that fields in the ‘required’ array must have data. It only means ‘required’ properties must be included in the data object, not that they have data in them. If you want to control what data is them, you’d give them a type, minLength/maxLength validations, etc. All that can be seen in the schema definition/validation documentation on the site I provided.

1reaction
brockfanningcommented, Nov 2, 2020

@kameelyan Aha, thanks very much. The problem you point out in my schema explains why required_by_default and show_opt_in did not appear to be changing anything.

I think I may be trying to use the library in a way that was not intended - to display a form to non-technical users. In my case, I want all the properties to appear, but I don’t want them to be required; and because the users are non-technical, I don’t want users to have to go into “Object properties” in order to see them (in fact to prefer to hide those buttons altogether).

I’m trying a workaround where I rely on default in the schema, in combination with custom code, in order to force these non-required properties to appear as form elements. However it’s not ideal.

I’m wondering if my use-case is too far outside the intended function of the library. If you agree, feel free to close this ticket. If not, do you think there should/could be a config setting which would force all properties to appear, even if unpopulated and non-required?

Read more comments on GitHub >

github_iconTop Results From Across the Web

<input>: The Input (Form Input) element - HTML
<input> types · button, A push button with no default behavior displaying the value of the value attribute, empty by default. · checkbox,...
Read more >
Don't submit form if inputs are empty - Stack Overflow
You wrap a form around all your inputs, give the form a name like name=form and set the required attribute on all inputs....
Read more >
Chapter 18 Forms and Form Fields - Eloquent JavaScript
The files property of a file field element is an array-like object (again, not a real array) containing the files chosen in the...
Read more >
Forms in HTML documents - W3C
Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the...
Read more >
ASP.NET Core Blazor forms and input components
For input components in a form with an EditContext, the default validation behavior includes updating the field CSS class to reflect the field's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found