A null option stores null
Detect
- In your schema: a property whose
oneOforanyOfoffers an option oftype: 'null'orenum: [null], the usual way to write a nullable field, or ofconst: nullortype: ['null']beside an owntypethat leavesnullout. - In your code: what reads the submitted data, such as
onSubmitoronChange, and expects no value for such a property. - In your schema: a
dependencies,dependentRequiredordependentSchemasentry for such a property.
{
"type": "object",
"required": ["nickname"],
"properties": {
"nickname": {
"oneOf": [
{ "title": "Text", "type": "string" },
{ "title": "None", "type": "null" }
]
}
}
}
Change
Picking the option set the property to undefined, so a required one couldn't be submitted. It now sets it to null, as an option written as const: null or type: ['null'] and an array item did (#5944). A property whose own type leaves null out, such as "type": "object" beside the options, and has no nullable: true, gets undefined however the option is written, where one written as const: null or type: ['null'] stored a null that failed validation.
A required property with no data that opens on such an option held nothing and failed validation until the user switched options and back. getDefaultFormState() and the form's starting data now hold null for it, unless that would add an optional object to the data. An optional property still starts absent.
Code that reads the data gets null where it got no value, and a dependencies, dependentRequired or dependentSchemas entry for the property now applies once the option is picked, as it does for any value. To send no value instead, remove it as you submit:
-onSubmit={({ formData }) => save(formData)}
+onSubmit={({ formData: { nickname, ...rest } }) => save(nickname === null ? rest : { ...rest, nickname })}
Verify
- Picking the
nulloption submitsnull, and a required property with it passes validation. - A required property that opens on the
nulloption starts atnull. - The dependencies of the property apply once the option is picked.
Also read: