Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null values cause warning in forms #8212

Merged
merged 3 commits into from
Sep 30, 2022
Merged

Fix null values cause warning in forms #8212

merged 3 commits into from
Sep 30, 2022

Conversation

fzaninotto
Copy link
Member

Closes #7782

Comment on lines +14 to +18
Object.keys(finalInitialValues).forEach(key => {
if (finalInitialValues[key] === null) {
finalInitialValues[key] = '';
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for nested values, like author.name for instance

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the fix requires a bit of recursion:

export default function getFormInitialValues(
    defaultValues: DefaultValue,
    record?: Partial<RaRecord>
) {
    const finalInitialValues = merge(
        {},
        getValues(defaultValues, record),
        record
    );
    // replace null values by empty string to avoid controlled/ uncontrolled input warning
    return mapValuesDeep(finalInitialValues, value =>
        value === null ? '' : value
    );
}

const mapValuesDeep = (obj, callback) =>
    mapValues(obj, v =>
        isPlainObject(v) ? mapValuesDeep(v, callback) : callback(v)
    );

But I'm afraid it could have nasty side effects. Why should we modify the inner properties of embedded objects - some of which my not be modifiable? Besides, the sanitizeEmptyValues doesn't work for nested properties, so the empty string would be sent back to the server.

All in all, this is another corner case, which already exists today, and I don't think this PR makes it worse.

Copy link
Contributor

@WiXSL WiXSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from @slax57 review seems good!

@slax57 slax57 added this to the 4.4.0 milestone Sep 30, 2022
@slax57 slax57 merged commit 76a2c88 into next Sep 30, 2022
@slax57 slax57 deleted the fix-null-values-warning branch September 30, 2022 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants