react-jsonschema-form
react-jsonschema-form copied to clipboard
Send empty string and null
Prerequisites
- [x] I have read the documentation
What theme are you using?
mui
What is your question?
Goal
- I want to allow empty string in a string single field.
- I want to allow empty string in string array values.
- I want to get data by
onChange()function when an element is added to an array. - For arrays of strings, I want to unify null values and undefined with empty string ("").
"dependencies": {
"@rjsf/core": "^5.24.8",
"@rjsf/mui": "^5.24.8",
"@rjsf/utils": "^5.24.8",
"@rjsf/validator-ajv8": "^5.24.8",
},
Issue
- Hard to figure out how to make it possible to send even an empty string.
- In the case of an array of strings, there is a pattern of empty and null value before and after editing, which causes bugs.
- For numeric arrays, there are undefined and null value patterns before and after editing.
- The data before editing must correspond to an empty, undefined, or null pattern in the called onChange() function.
- To unify with empty string, null or undefined in received data must be set to empty string.
- In the case of an array of strings, set the
defaultproperty to""will unify empty strings with null values and allow them to be sent before editing. - In the case of an array of numbers, setting the
defaultproperty tonullwill unify undefined and null values and allow them to be sent before editing. - If the
defaultproperty is not defined, even for an array of strings, the value may be undefined before editing.
How to send an empty string
const schema = {
type: ["string", "null"],
default: "",
};
- Single field returns
“”. - For array elements, returns
“”before editing,nullafter editing.
const schema = {
type: ["number", "null"],
default: null,
};
- In the case of an array of numbers, adding a new element causes the value to be undefined, and an empty string after editing becomes null.
Other patterns
const schema = {
type: ["string", "null"],
};
- In the live demo, FormData returns
nullboth before and after editing. - In my app, it returns
undefinedbefore editing andnullafter editing. - I get a
must be string,nullerror when I try to submit before editing.
const schema = {
type: ["string", "null"],
default: null,
};
- FormData is
nullboth before and after editing in the live demo and in my app. - I get a
must be string,nullerror when I try to submit before editing.
const schema = {
type: ["number", "null"],
};
- In the case of an array of numbers, adding a new element causes the value to be
undefined, and an empty string after editing becomesnull. - I get a
must be string,nullerror when I try to submit before editing.
Other patterns we have tested include the following, but to no avail:
- required: []
- minLength: 0
- Form Component
- noValidate
- noHTML5Validate
- UI Schema
"ui:emptyValue": ""
See also:
- https://github.com/rjsf-team/react-jsonschema-form/issues/289