Angular 2: Form submission canceled because the form is not connected
There might be other reasons this occurs but in my case I had a button that was interpreted by the browser as a submit button and hence the form was submitted when the button was clicked causing the error. Adding type="button" fixed the issue. Full element:
<button type="button" (click)="submitForm()">
React form error: Form submission canceled because the form is not connected
The browser's default handling of a form submission is to make an HTTP POST request to the server and reload the page with whatever is in the response (which may or may not even be content the browser can render). This is almost never what you want in a single page application, because your entire app and all of its state is simply discarded.
Instead of wiring up the form submit as a click handler on the submit button, you should instead wire it up as the onSubmit
event of the form. You also want to call preventDefault()
on the event, which prevents the native browser behavior. The button doesn't need a click handler at all as long as it has the attribute type="submit"
(presumably, inside of your Submit
component, there is a regular button
element).
formSubmit(event) {
event.preventDefault();
// your submit logic
}
render() {
// ...
<form onSubmit={(event) => this.formSubmit(event)}
// ... Inside `Submit` should be something like:
<button type="submit" />
</form>
}
Form submission canceled because the form is not connected in react js. Where am I going wrong?
I suspect your code is submitting the form and some default form actions are occurring. This is because button
elements are of type="submit"
by default when no type
attribute is provided, and the form
element has no onSubmit
handler to prevent the default form actions from occurring. When the default form action occurs, generally the page will reload, this is likely wiping out your state update.
Explicitly declare the button to be type="submit"
and move the handleSubscription
callback to the form
element's onSubmit
handler. In handleSubscription
prevent the default submit action.
handleSubscription = (event) => {
event.preventDefault();
this.setState({
isLoading: true,
redeemVoucherForm: false,
error: false,
verified: false
});
};
...
<form
className={styles.redeem_form}
onSubmit={handleSubscription}
>
...
<button
className={styles.avail_subs}
disabled={!this.state.voucherNumber || !this.state.pin}
>
Avail Subscription
</button>
</form>
Form submission canceled because the form is not connected - Console error
I just change the button type into button and not submit, and IT WORKS
<Button type="button" onClick={onSubmit} className="btn btn-success">Submit</Button>
Error: Form submission canceled because the form is not connected, React.js
You can call preventDefault
inside the onsubmit
handler:
const onsubmit = (e) => {
e.preventDefault();
props.onadd({
title: getTitle,
image: getImage,
rating: getRating
});
}
Getting Error Form submission canceled because the form is not connected
Quick answer : append the form to the body.
document.body.appendChild(form);
Or, if you're using jQuery as above
$(document.body).append(form);
Details :
According to the HTML standards, if the form is not associated to the browsing context(document), the form submission will be aborted.
HTML SPEC see 4.10.21.3.2
In Chrome 56, this spec was applied.
Chrome code diff see @@ -347,9 +347,16 @@
P.S about your question #1. In my opinion, unlike ajax, form submission causes instant page move.
So, showing 'deprecated warning message' is almost impossible.
I also think it's unacceptable that this serious change is not included in the feature change list. Chrome 56 features - www.chromestatus.com/features#milestone%3D56
Related Topics
Pass Arguments with Page.Evaluate
Browser Event When Downloaded File Is Saved to Disk
Short-Polling VS Long-Polling for Real Time Web Applications
How to Filter Array When Object Key Value Is in Array
Maximum Call Stack Size Exceeded on Npm Install
How to Create Document Objects with JavaScript
Understanding Ajax Cors and Security Considerations
Componentdidmount Equivalent on a React Function/Hooks Component
Function to Convert Timestamp to Human Date in JavaScript
Uncaught Typeerror: Cannot Read Property 'Value' of Undefined
React Router Doesn't Work in Aws S3 Bucket
How to Iterate (Keys, Values) in JavaScript
How to Check Whether <Ng-Content> Is Empty? (In Angular 2+ Till Now)
How to Use Command Line Arguments in Angularjs Protractor
How to Store a JavaScript Function in JSON
What Is Difference Between Axios and Fetch
How to Get the Current Time Only in JavaScript
Firebase Query Methods Startat() Taking Case Sensitive Parameters