React-jsonschema-form: default value does not work

Created on 12 Jul 2018  路  11Comments  路  Source: rjsf-team/react-jsonschema-form

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "No"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number"
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              **"default": false;**
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

as shown in the code, the default value for "Do you want to get rid of any?" does not work!

Most helpful comment

my guess is that OP is running into the same problem i am. if the initial formData contains any value other than undefined for a property (like null), default will be ignored.

that's correct, but unexpected, especially when dealing with non-required fields.

IMO -- null for non-required fields should be stripped and not fail validation, since there is no way for an end-user to enter a value of null.

json schema supports multiple types e.g. something: { type: [ 'string', 'null' ] }. if this lib supported that, this would be a non-issue.

but as it stands right now, you need to preprocess data and strip all null values before handing it off to jsonschema-form or else it'll force your user to interact with a non-required field.

An example for https://mozilla-services.github.io/react-jsonschema-form/

{
  "type": "object",
  "properties": {
    "notrequired": {
      "type": "string",
      "title": "Not required",
      "minLength": 0,
      "default": "fdsa"
    }
  }
}

formData

{
  "notrequired": null
}

With that, there is no way to submit the form.

This uischema doesn't help matters either: { "notrequired": { "ui:emptyValue": "" } }

All 11 comments

Hi @KunWangV , You have a typo in your schema definition the character ; in the "default" definition of "Do you want to get rid of any?" . Apart from that it is working the default value as expected.

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "No"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number"
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              "default": false
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

Copy and paste that on https://mozilla-services.github.io/react-jsonschema-form/ , it works.

Regards

I cannot get the expected result using your code, are you sure it is ok?

my guess is that OP is running into the same problem i am. if the initial formData contains any value other than undefined for a property (like null), default will be ignored.

that's correct, but unexpected, especially when dealing with non-required fields.

IMO -- null for non-required fields should be stripped and not fail validation, since there is no way for an end-user to enter a value of null.

json schema supports multiple types e.g. something: { type: [ 'string', 'null' ] }. if this lib supported that, this would be a non-issue.

but as it stands right now, you need to preprocess data and strip all null values before handing it off to jsonschema-form or else it'll force your user to interact with a non-required field.

An example for https://mozilla-services.github.io/react-jsonschema-form/

{
  "type": "object",
  "properties": {
    "notrequired": {
      "type": "string",
      "title": "Not required",
      "minLength": 0,
      "default": "fdsa"
    }
  }
}

formData

{
  "notrequired": null
}

With that, there is no way to submit the form.

This uischema doesn't help matters either: { "notrequired": { "ui:emptyValue": "" } }

Any updates here? I have a complex form that starts with a single drop down box. The form is populated based off of what is selected using the dependencies. I have several default values that need to be pre-populated. Anyone know how to get the default value to work here?

I think this issue is not really coherent so I'm going to close it.

As mentioned by @doncesarts, when I fix the typo in the JSON schema, it seems to work OK (an empty formData becomes {"Do you have any pets": "No"}, and selecting Yes: More than one auto-fills "Do you want to get rid of any?": false).

@cmawhorter: default values aren't applied when existing values are present. Yes, that includes null, false, empty arrays, or any other JSON-allowed value. I don't really expect that behavior to change. rjsf generally doesn't try to "clean" data.

@jduncanRadBlue, if your issue is not addressed by the above remarks, maybe it would be better to open a new issue.

@glasserc The change @doncesarts mentioned does not work. I copy the typo-less schema into the live editor and hit submit and result: {}. It should be: { "Do you have any pets?": "No" }, because No is the default.

I'm not talking about sanitizing the data as much as I'm talking about a limitation in this lib that can't be worked around.

Getting back to the pets schema. The default value for "Do you have any pets?" is pointless because it's impossible to ever use it (as far as rjsf is concerned).

@glasserc The change @doncesarts mentioned does not work for me either. The part that I'm concerned with is the dependencies. I need to be able to specify default values for those fields. I am not able to get those default values to render.

@cmawhorter I just tried pasting the typo-less schema into the playground and replacing the formData with {} and it was immediately replaced with

{
  "Do you have any pets?": "No"
}

Clicking Submit caused it to log exactly the same object to the log. It sounds like you're seeing something else so please feel free to open another issue to describe what exactly you're doing and what exactly you're seeing.

@jduncanRadBlue Here is the same pet schema with a schema dependency on the field which has a default as well as providing a default.

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "Yes: One"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number",
              "default": 21
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              "default": false
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

Replacing the formData with {} caused it to produce

{
  "Do you have any pets?": "Yes: One",
  "How old is your pet?": 21
}

As previously mentioned, since this issue doesn't really make sense, please feel free to open a new one.

@glasserc I seem to have a similar example, but I'm unable to make the dependent properties show with the default values. Can you let me know if this is due to an issue with the react-jsonschema-form or with my schema?

Specifically, the below can be parsed in the PlayGround. Upon selecting Configuration Mode to be "Simple", I'd expect the default value of "Bit-rate standard" to be 250000 (i.e. 250K) - but it's simply the first entry in the available list. Further, no value is added to the formData until I manually select another value for "Bit-rate standard".

Any thoughts/inputs would be appreciated,
Martin

{
  "type": "object",
  "properties": {
    "can": {
      "type": "object",
      "properties": {
        "phy": {
          "title": "",
          "type": "object",
          "properties": {
            "bit_rate_cfg_mode": {
              "title": "Configuration mode",
              "type": "integer",
              "default": 1,
              "anyOf": [
                {
                  "title": "Auto",
                  "enum": [
                    1
                  ]
                },
                {
                  "title": "Simple",
                  "enum": [
                    2
                  ]
                }
              ]
            }
          },
          "dependencies": {
            "bit_rate_cfg_mode": {
              "oneOf": [
                {
                  "properties": {
                    "bit_rate_cfg_mode": {
                      "enum": [
                        1
                      ]
                    }
                  }
                },
                {
                  "properties": {
                    "bit_rate_cfg_mode": {
                      "enum": [
                        2
                      ]
                    },
                    "bit_rate_std": {
                      "title": "Bit-rate standard",
                      "type": "integer",
                      "default": 250000,
                      "anyOf": [
                        {
                          "title": "5K",
                          "enum": [
                            5000
                          ]
                        },
                        {
                          "title": "10K",
                          "enum": [
                            10000
                          ]
                        },
                        {
                          "title": "20K",
                          "enum": [
                            20000
                          ]
                        },
                        {
                          "title": "33.333K",
                          "enum": [
                            33333
                          ]
                        },
                        {
                          "title": "47.619K",
                          "enum": [
                            47619
                          ]
                        },
                        {
                          "title": "50K",
                          "enum": [
                            50000
                          ]
                        },
                        {
                          "title": "83.333K",
                          "enum": [
                            83333
                          ]
                        },
                        {
                          "title": "95.238K",
                          "enum": [
                            95238
                          ]
                        },
                        {
                          "title": "100K",
                          "enum": [
                            100000
                          ]
                        },
                        {
                          "title": "125K",
                          "enum": [
                            125000
                          ]
                        },
                        {
                          "title": "250K",
                          "enum": [
                            250000
                          ]
                        },
                        {
                          "title": "500K",
                          "enum": [
                            500000
                          ]
                        },
                        {
                          "title": "800K",
                          "enum": [
                            800000
                          ]
                        },
                        {
                          "title": "1M",
                          "enum": [
                            1000000
                          ]
                        }
                      ]
                    },
                    "bit_rate_fd": {
                      "title": "Bit-rate FD",
                      "type": "integer",
                      "default": 1,
                      "anyOf": [
                        {
                          "title": "Disable",
                          "enum": [
                            1
                          ]
                        },
                        {
                          "title": "1M",
                          "enum": [
                            1000000
                          ]
                        },
                        {
                          "title": "2M",
                          "enum": [
                            2000000
                          ]
                        },
                        {
                          "title": "5M",
                          "enum": [
                            4000000
                          ]
                        }
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

@MatinF thanks for your report, it does seem to be an issue with using default and oneOf/anyOf. Can you please make a playground link (https://mozilla-services.github.io/react-jsonschema-form/) with your example and make another issue for this?

@epicfaace I've now added this as a new issue with an example as requested:
https://github.com/mozilla-services/react-jsonschema-form/issues/1293

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anttivikman picture anttivikman  路  3Comments

pablen picture pablen  路  3Comments

mfulton26 picture mfulton26  路  3Comments

norim13 picture norim13  路  3Comments

jabaren picture jabaren  路  3Comments