react icon indicating copy to clipboard operation
react copied to clipboard

Bug: missing button data in form when submitted via `formAction`

Open mattcarrollcode opened this issue 1 year ago • 11 comments

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 avatar Sep 18 '23 20:09 mattcarrollcode

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

mattcarrollcode avatar Sep 19 '23 20:09 mattcarrollcode

import React, { useRef } from "react";

const Component = () => { const submitButtonRef = useRef(null);

function action(formData) { console.log(formData.get("n1")); }

function handleSubmit(event) { event.preventDefault(); submitButtonRef.current.click(); }

return ( <> <button type="button" onClick={handleSubmit}> Submit

</> ); };

export default Component;

SiddiqAM1 avatar Sep 20 '23 09:09 SiddiqAM1

Use a hidden submit button inside of the form tag and programmatically click it when the external button is clicked.

SiddiqAM1 avatar Sep 20 '23 09:09 SiddiqAM1

you can also use React Hook Form Library

SiddiqAM1 avatar Sep 20 '23 09:09 SiddiqAM1

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 avatar Sep 22 '23 20:09 mattcarrollcode

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

https://github.com/facebook/react/blob/e3748a0bde80dd1f097fd8000702aba9fca454ef/packages/react-dom-bindings/src/events/plugins/FormActionEventPlugin.js#L83-L97

sophiebits avatar Oct 19 '23 06:10 sophiebits

@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.

AlejandroBlanco2001 avatar Nov 09 '23 04:11 AlejandroBlanco2001

This is an auto-reply email.Hi, due to the high volume of emails, I will get back to you as soon as possible within three days.

lllomh avatar Nov 09 '23 04:11 lllomh

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? 🙏

jupapios avatar Jan 23 '24 02:01 jupapios

This is an auto-reply email.Hi, due to the high volume of emails, I will get back to you as soon as possible within three days.

lllomh avatar Jan 23 '24 02:01 lllomh

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[bot] avatar Apr 24 '24 00:04 github-actions[bot]