Skip to main content
Version: Current (v7.0.0-beta.3)

A list of choices with no default starts empty

Detect​

  • In your schema: a field whose oneOf or anyOf options are two or more constants, the titled way to write an enum, with no default, its own or one an ancestor's default gives it. That includes boolean, object and array constants, a list an allOf brings in, and such a field at the root, as an array item, a tuple position or an additional property.
  • In your schema: a required boolean whose listed values leave out false, such as { "type": "boolean", "enum": [true] }.
  • In your form data: no value for that field, a NaN, or a null at the root or under mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined'.
  • On the rendered page: a select, radio group or checkbox that started on its first option now starts empty.

Change​

Such a oneOf or anyOf started on its first constant, so an optional select nobody touched submitted the first option. It now starts empty, as an enum does, on the select's empty option (#5944). With this schema and no formData, getDefaultFormState() and the form's starting data were { "color": "red" } and are now {}:

{
"type": "object",
"properties": {
"color": {
"type": "string",
"oneOf": [
{ "const": "red", "title": "Red" },
{ "const": "green", "title": "Green" }
]
}
}
}
  • The options are counted rather than their values, so a constant listed twice, such as true under two titles, is a list of two and starts empty too.
  • Whatever control renders the list starts empty: a checkbox over boolean constants without titles starts unticked, and a radio group with none checked.
  • A required field then fails validation until the user picks an option, and an array padded to its minItems gets empty rows, as for enum items.
  • A NaN in the form data of such a field, or a null at the root or under mergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined', which the first constant replaced, is now handled as for an enum. A required boolean whose listed values include false takes false in its place, as below, and any other field keeps it as it is.
  • A oneOf or anyOf whose one option is a const, such as [{ "const": "a" }], leaves nothing to choose and still starts on that constant. One whose one option is an enum, such as [{ "enum": ["a"] }], still starts empty, as an enum does.
  • A field's own const still fills, and so does the const of a property inside the option a field shows, such as a kind that tells object options apart. An enum still starts empty, however many values it lists.

A required boolean with such a list is left unpicked too, and then starts at false, as any required boolean does under requiredBooleanDefault, whatever control renders it. A required { "type": "boolean", "oneOf": [{ "const": true, "title": "Yes" }, { "const": false, "title": "No" }] } started at true and now starts at false, its select on "No", or its checkbox unticked where the constants have no title. An optional one starts empty. To leave a required one unanswered until the user picks, set requiredBooleanDefault to 'skip'.

A required boolean whose listed values leave out false is left empty instead, so validation reports it missing until the user picks. Its listed values are those of its enum, its const, either of them brought in by an allOf or by the branch its if takes, its list of constants, or, where its oneOf/anyOf options aren't all constants, the enum, const or list of constants of the option it starts on. A required { "type": "boolean", "enum": [true] } started at false and now starts empty. So does a required { "type": ["boolean", "null"], "oneOf": [{ "const": true, "title": "Yes" }, { "const": null, "title": "N/A" }] }, which started at true, and a required { "type": "boolean", "anyOf": [{ "enum": [true] }, { "title": "Any" }] }, which starts on its first option and started at false. To keep the old data, make it the field's default, as "default": false for the enum and "default": true for the list. No requiredBooleanDefault value brings it back, since 'skip' leaves every required boolean empty.

To start from a value again, make it the field's default:

"color": {
"type": "string",
+ "default": "red",
"oneOf": [

To start every constant oneOf and anyOf of a form from its first constant, set constAsDefaults to 'always', which also lets a const replace a value the form data holds:

-<Form schema={schema} validator={validator} />
+<Form schema={schema} validator={validator} defaultFormStateBehavior={{ constAsDefaults: 'always' }} />

Verify​

  • An optional list of choices nobody touched is absent from the submitted data.
  • Every required field the user can't change, such as a hidden (ui:widget: 'hidden'), readOnly or disabled one, has a default. It used to submit its first constant without the user seeing it. It now starts empty, and every submit fails with "must have required property", which the user has no way to fix.
  • A required boolean starts at the value you expect, or empty where its listed values leave out false.

Also read: