-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds JavaScript to prevent multiple form submissions (#345)
- Disables the submit button on forms after click [#185364171]
- Loading branch information
1 parent
f598b6e
commit d643219
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...ources/META-INF/resources/webjars/form-flow/0.0.1/js/formFlowDisableMultipleFormSubmit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const formSubmitButton = document.getElementById("form-submit-button"); | ||
if (formSubmitButton) { | ||
const form = formSubmitButton.form; | ||
if (form) { | ||
form.addEventListener("submit", function () { | ||
formSubmitButton.classList.add("button--disabled"); | ||
formSubmitButton.disabled = true; | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/java/formflow/library/client/DisableMultipleFormSubmitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package formflow.library.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
import formflow.library.utilities.AbstractBasePageTest; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import org.openqa.selenium.support.ui.ExpectedConditions; | ||
import org.openqa.selenium.support.ui.WebDriverWait; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest(properties = {"form-flow.path=flows-config/test-flow.yaml"}, webEnvironment = RANDOM_PORT) | ||
public class DisableMultipleFormSubmitTest extends AbstractBasePageTest { | ||
|
||
@Override | ||
@BeforeEach | ||
public void setUp() throws IOException { | ||
startingPage = "flow/testFlow/pageWithDefaultSubmitButton"; | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
void shouldNotSubmitFormMultipleTimes() { | ||
WebElement formSubmitButton = testPage.findElementById("form-submit-button"); | ||
WebElement form = driver.findElement(By.tagName("form")); | ||
driver.executeScript("arguments[0].addEventListener('submit', function(event) { event.preventDefault(); });", form); | ||
formSubmitButton.click(); | ||
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); // wait up to 5 seconds | ||
wait.until(ExpectedConditions.attributeToBe(formSubmitButton, "disabled", "true")); | ||
assertThat(formSubmitButton.isEnabled()).isFalse(); | ||
} | ||
} |