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

Add automated empty values sanitization #8188

Merged
merged 17 commits into from
Sep 30, 2022
Merged

Add automated empty values sanitization #8188

merged 17 commits into from
Sep 30, 2022

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Sep 22, 2022

Problem

All form inputs have an empty value of '' (empty string). React-hook-form doesn't sanitize these values by default. This leads to unexpected create and update payloads like:

{
  id: 123,
  title: '',
  author: '',
}

The currently documented solution is to use parse on individual inputs, and/or transform at the controller level.

But none of these solutions is particularly convenient when developers have to use them everywhere. The input-level solution is super repetitive, and the controller-one is at the wrong level - the problem is caused by the form, it should be handled by the form.

Solution

Introduce an opt-in <Form sanitizeEmtyValues> prop

  <Edit>
    <SimpleForm sanitizeEmptyValues>
      <TextInput source="title" />
    </SimpleForm>
  </Edit>
);

Todo

  • Create an empty value sanitizer and test it
  • Add the support for sanitizeEmptyValues in , , and ``
  • Document the new prop
  • Remove advice to use transform to sanitize empty values
  • In Reference inputs, sanitize empty values by default

@fzaninotto fzaninotto added the WIP Work In Progress label Sep 22, 2022
@fzaninotto fzaninotto changed the base branch from master to next September 22, 2022 11:54
@fzaninotto fzaninotto marked this pull request as ready for review September 29, 2022 10:12
@slax57 slax57 added RFR Ready For Review and removed WIP Work In Progress labels Sep 29, 2022
docs/Form.md Show resolved Hide resolved
docs/ReferenceInput.md Outdated Show resolved Hide resolved
docs/ReferenceInput.md Outdated Show resolved Hide resolved
packages/ra-core/src/form/useAugmentedForm.ts Show resolved Hide resolved
fzaninotto and others added 3 commits September 29, 2022 13:07
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
docs/ReferenceInput.md Outdated Show resolved Hide resolved
Co-authored-by: Aníbal Svarcas <WiXSL@users.noreply.github.com>
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.

Can you rebase to include #8162?
We may have to change the note in the docs of AutocompleteInput and SelectInput regarding emptyValue prop being null

@fzaninotto
Copy link
Member Author

Back to WIP as this makes #7782 more pregnant

@fzaninotto
Copy link
Member Author

Back to RFR - we'll fix #7782 in a bug fix release, no need to delay the next minor.

@fzaninotto fzaninotto added RFR Ready For Review and removed WIP Work In Progress labels Sep 30, 2022
docs/AutocompleteInput.md Outdated Show resolved Hide resolved
docs/SelectInput.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants