react
react copied to clipboard
Bug: missing button data in form when submitted via `formAction`
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:
React version: 0.0.0-experimental-2807d781a-20230918
Steps To Reproduce
- 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
andvalue
attributes set with non-null values. The action should take aformData
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> </> ); }
- 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
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
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;
Use a hidden submit button inside of the form tag and programmatically click it when the external button is clicked.
you can also use React Hook Form Library
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
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
@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.
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.
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? 🙏
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.
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!