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

Bug: missing button data in form when submitted via formAction #27391

Closed
mattcarrollcode opened this issue Sep 18, 2023 · 11 comments · Fixed by #28056
Closed

Bug: missing button data in form when submitted via formAction #27391

mattcarrollcode opened this issue Sep 18, 2023 · 11 comments · Fixed by #28056
Labels
Component: DOM React Core Team Opened by a member of the React Core Team Type: Bug

Comments

@mattcarrollcode
Copy link
Contributor

When a form action is triggered by a button that is outside the form's tag the button's name and value is missing from the form data provided to the action. When the button submitting the form is inside the form tag the button's form data is properly surfaced to the action:

output

React version: 0.0.0-experimental-2807d781a-20230918

Steps To Reproduce

  1. Create a form that calls an action when submitted with a button outside the form tag submits the form. The button that submits the form should have the name and value attributes set with non-null values. The action should take a formData argument and log the button's value:
    function Component() {
      function action(formData) {
        console.log(formData.get("n1"));
      }
      return (
        <>
          <button type="submit" name="n1" value="v1" form="form-id">
            Submit
          </button>
          <form action={action} id="form-id"></form>
        </>
      );
    }
    
  2. Click the button that submits the form

Link to code example: https://codesandbox.io/s/flamboyant-meadow-gcvpx9?file=/App.js

The current behavior

The formData provided to the form action doesn't contain the key-value pair information of the button that submitted the form when the button is outside the form's tag. This means that nothing is logged because the n1 key doesn't exist in formData

The expected behavior

The value of the external button to be logged (v1 in the example above) because formData includes the

@mattcarrollcode mattcarrollcode added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Sep 18, 2023
@mattcarrollcode
Copy link
Contributor Author

mattcarrollcode commented Sep 19, 2023

Standard HTML forms provide the name and value of the button that submitted the form even if the button is outside the form tag: https://codesandbox.io/p/sandbox/practical-smoke-h6s2wp?file=%2Fapp.js%3A11%2C24

@SiddiqAM1

This comment was marked as off-topic.

@SiddiqAM1

This comment was marked as off-topic.

@SiddiqAM1

This comment was marked as off-topic.

@mattcarrollcode
Copy link
Contributor Author

This also seems to be a problem when using formAction on a button inside the form. If you have a button that has the formAction attribute the name and value of that button are not surfaced in the form data reguardless of whether the button is inside the form or outside the form.

Code example comparing a "default" submit button (which does surface the button's form data properly) and a button with the formAction attribute set with a client action handler: https://codesandbox.io/s/hungry-bose-j6tv7p?file=/App.js

@mattcarrollcode mattcarrollcode changed the title Bug: missing form action data when submitting via an external button Bug: missing button data in form when submitted via formAction Sep 22, 2023
@sophiebits
Copy link
Collaborator

Code pointer for debugging – it's meant to be added here:

if (submitter) {
// The submitter's value should be included in the FormData.
// It should be in the document order in the form.
// Since the FormData constructor invokes the formdata event it also
// needs to be available before that happens so after construction it's too
// late. We use a temporary fake node for the duration of this event.
// TODO: FormData takes a second argument that it's the submitter but this
// is fairly new so not all browsers support it yet. Switch to that technique
// when available.
const temp = submitter.ownerDocument.createElement('input');
temp.name = submitter.name;
temp.value = submitter.value;
(submitter.parentNode: any).insertBefore(temp, submitter);
formData = new FormData(form);
(temp.parentNode: any).removeChild(temp);

@AlejandroBlanco2001
Copy link

@mattcarrollcode I have faced this similar issue when building forms with the button outside the <form> tag and using the attribute form inside the button with React. I think that in your example works because is pure HTML, but as for as I understand this situations is due on how React handles this event, to overcome this I always use onSubmit event.

Maybe have a deeper look on the code pointed by @sophiebits would give more clarity.

@lllomh

This comment was marked as off-topic.

jupapios added a commit to jupapios/react that referenced this issue Jan 23, 2024
Fixes facebook#27391

`form-associated elements` can have a form owner, the associated form can be anywhere in the document.
jupapios added a commit to jupapios/react that referenced this issue Jan 23, 2024
Fixes facebook#27391

`form-associated elements` can be associated with `<form>`s anywhere in the document (even if the element is outside the `<form>`), and just like regular submitters, their name and value are expected to be reflected in the final `FormData`.
jupapios added a commit to jupapios/react that referenced this issue Jan 23, 2024
Fixes facebook#27391

`form-associated elements` can be associated with `<form>`s anywhere in the document (even if the element is outside the `<form>`), and as with any submitter, the `name` and `value` are expected to be reflected in the final `FormData`.
@jupapios
Copy link
Contributor

jupapios commented Jan 23, 2024

I just created a PR that should fix the original issue, @mattcarrollcode regarding the second issue, it seems there is a test that expects such behavior, so I guess that, for now, it is expected, but I might be wrong, @sophiebits can you confirm, please? 🙏

@lllomh
Copy link

lllomh commented Jan 23, 2024 via email

Copy link

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 24, 2024
@eps1lon eps1lon added Type: Bug Component: DOM React Core Team Opened by a member of the React Core Team and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Resolution: Stale Automatically closed due to inactivity labels Apr 24, 2024
jupapios added a commit to jupapios/react that referenced this issue May 1, 2024
Fixes facebook#27391

`form-associated elements` can be associated with `<form>`s anywhere in the document (even if the element is outside the `<form>`), and as with any submitter, the `name` and `value` are expected to be reflected in the final `FormData`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DOM React Core Team Opened by a member of the React Core Team Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants