question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: Add `form` property to IonButton to submit from outside form

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
itsLucariocommented, Sep 25, 2020

Hi @liamdebeasi ,

I have created PR which addresses the feature request and enables form submission even outside the form with added form property.

2reactions
postnerdcommented, Sep 11, 2022

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

  1. implement string support in ion-button for form prop
  2. expand form prop support to HTMLFormElement references

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found