Skip to content

Form state dirty does stays true when cancelling the selection of a file #1704

@FernandoFigueroa

Description

@FernandoFigueroa

Describe the bug

On an input type file, when cancelling the selection, the value of the registered fields goes from blank|null|undefined to a FileList with length 0, since we can't initialize something with a FileList, i guess that means that like the new value is different from the initial one, the field remain dirty.

I tried to workaround by using dirtyFields, checking that if the field was there and no value is set, i delete it from the set, however, that does not update isDirty.

I have it working by replacing the formState.dirty check on my submit button with:

 const formDirty = () => {
    if (dirtyFields.has('fileInput')) {
      const { fileInput } = getValues();

      if (!fileInput || !fileInput.length) {
        dirtyFields.delete('fileInput');
      }
    }

    return Boolean(dirtyFields.size);
  };

That basically relies on dirtyFields.size, but i guess that is something should be handled internally.

To Reproduce
Steps to reproduce the behavior:

  1. Go to: https://codesandbox.io/s/react-hook-form-useform-template-qh1r8?file=/src/index.js
  2. Click on "choose file"
  3. Click cancel on the browser upload dialog
  4. See the submit enabled, the form is dirty

Codesandbox link (Required)

https://codesandbox.io/s/react-hook-form-useform-template-qh1r8?file=/src/index.js

Expected behavior
The form should not be dirty, button should not be enabled.

Desktop (please complete the following information):

  • OS: Mac
  • Browser: Chrome
  • Version: Latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions