Cannot use two inputs with the same name
See original GitHub issueDescribe the bug I cannot use two inputs with the same name, that I switch between using a checkbox.
To Reproduce
const { handleSubmit, register, watch } = useForm();
const toggle = watch("toggle");
const onSubmit = data => {
alert(JSON.stringify(data, null, 2));
};
return (
<form className="App" onSubmit={handleSubmit(onSubmit)}>
<input name="toggle" ref={register} type="checkbox" />
{toggle ? (
<div>
<h1>Something here</h1>
<input name="someValue" ref={register} />
</div>
) : (
<div>
<input name="someValue" ref={register} />
<h1>Something HERE</h1>
</div>
)}
<button type="submit">submit</button>
</form>
);
The <input name="someValue" />
gets unmounted and re-rendered - the new one doesn’t work. Only the original one works (when the checkbox is unchecked).
Codesandbox link (Required) https://codesandbox.io/s/friendly-chaum-w4tbj
Expected behavior I expect the second input to work just as the first one does.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Is it valid to have two input elements with the same name?
Yes, it is valid. This is Good <form name="form1"> <input type="hidden" name="url" value="1"> </form> <form name="form2"> <input ...
Read more >Cannot map the same column to two inputs of a flowlet
I have tried to create similar Flowlet configuration and could find that by design we could not map same column to two inputs...
Read more >How to Use Fields | Form Designer | User Guide | Epi Info
The field name cannot be changed after the data table is created. When testing a form in Enter, the data table is initially...
Read more >Forms in HTML documents
Several checkboxes in a form may share the same control name. Thus, for example, checkboxes ... The INPUT element is used to create...
Read more ><input type="checkbox"> - HTML: HyperText Markup Language
In this example you will see that we've given each checkbox the same name . If both checkboxes are checked and then the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is expected behavior, an alternative solution will be
unregister
the field manually.let me know if this solves your problem.
Or having a different input on mobile vs desktop and using css to hide one or the other…