-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3758 from marmelab/forms-empty-values
[RFR] Fix Forms Remove Empty Values
- Loading branch information
Showing
7 changed files
with
87 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import merge from 'lodash/merge'; | ||
|
||
/** | ||
* Because final-form removes undefined and empty string values completely | ||
* (the key for the empty field is removed from the values), we have to check | ||
* wether this value was initially provided so that it is correctly sent to | ||
* the backend. | ||
* See https://github.com/final-form/react-final-form/issues/130#issuecomment-493447888 | ||
* | ||
* @param initialValues The initial values provided to the form | ||
* @param values The current form values | ||
*/ | ||
const sanitizeEmptyValues = (initialValues: object, values: object) => { | ||
// For every field initialy provided, we check wether it value has been removed | ||
// and set it explicitly to an empty string | ||
const initialValuesWithEmptyFields = Object.keys(initialValues).reduce( | ||
(acc, key) => { | ||
if (typeof values[key] === 'object' && values[key] !== null) { | ||
acc[key] = sanitizeEmptyValues(initialValues[key], values[key]); | ||
} else { | ||
acc[key] = | ||
typeof values[key] === 'undefined' ? '' : values[key]; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
|
||
// Finaly, we merge back the values to not miss any which wasn't initialy provided | ||
return merge(initialValuesWithEmptyFields, values); | ||
}; | ||
|
||
export default sanitizeEmptyValues; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters