feat: Add `form` property to IonButton to submit from outside form
See original GitHub issueBug Report
Ionic version: 4.x
Current behavior: IonButton does not trigger submission of the form. You need this when your submit button is outside the form itself.
<form id="some-form">....</form>
<ion-button form="some-form">...</ion-button>
Expected behavior: Should trigger the submit event of the form like so: https://www.w3schools.com/tags/att_button_form.asp
Steps to reproduce:
Add the code below:
<form id="some-form" (ngSubmit)="submit('Triggered')"></form>
<ion-button form="some-form">Trigger submit</ion-button>
Tap on the button and nothing will happen
Related code:
https://stackblitz.com/edit/ionic-v4-angular-tabs-qx5u9v?file=src%2Fapp%2Ftab1%2Ftab1.page.html
Workaround: Hide native button element and trigger it by reference
...<form ... id="transaction-form">...</form>
...
<ion-footer>
<ion-toolbar button (click)="submitBtn.click()">
<ion-title>Some title</ion-title>
<button hidden #submitBtn type="submit" form="transaction-form"></button>
</ion-toolbar>
</ion-footer>
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:16 (7 by maintainers)
Top Results From Across the Web
Unable to submit form with button outside of content (Ionic 4)
We need to use the safe navigation operator, since when template is rendered, we do not yet have access to the form variable....
Read more >Submit A Form With A Button Outside The Form
The trick is to give the form an id and then reference that id with the button's form property. With this setup, clicking...
Read more >Use a Submit Button Outside of a Form! - David Walsh Blog
To submit a form when the button isn't a child of the parent form, you can use the form attribute: <form id="myForm"> <label ......
Read more >Using React Hook Form with Ionic React Components
We get the value from the IonRange component using the selected.detail.value property and set the object appropriately and let the react-hook- ...
Read more >ion-button - Ionic Framework
Design and style button elements with custom CSS properties. ... Buttons provide a clickable element, which can be used in forms, or anywhere...
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 FreeTop 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
Top GitHub Comments
Hi @liamdebeasi ,
I have created PR which addresses the feature request and enables form submission even outside the form with added
form
property.@sean-perkins I’ve created a PR yesterday (after I was facing a similar challenge with an app I am working on). I tried to follow your instructions and also added support for referencing a HTMLFormElement. But since this is my first PR for this project, I’m not quite sure if my solution fits your ideas.
Especially my implementation of the HTMLFormElement is maybe a little off, since I’m not even sure if the form prop can still be a HTMLFormElement at the time of runtime of the handleClick function.
If there is a larger implementation needed to support the HTMLFormElement referencing idea maybe we can split this feature request in two parts:
If so I would reduce the scope of my PR and we can create a new issue for the second part. Since I’m pretty sure, that the string support would do the trick for 90% of the people (and we can stop using shadow buttons or other workarounds).
Please let me know if I should add/change something.