Skip to content

Commit

Permalink
Adds JavaScript to prevent multiple form submissions (#345)
Browse files Browse the repository at this point in the history
- Disables the submit button on forms after click

[#185364171]
  • Loading branch information
spokenbird authored Aug 8, 2023
1 parent f598b6e commit d643219
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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;
});
}
}
});
1 change: 1 addition & 0 deletions src/main/resources/templates/fragments/libraryHead.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
<script src="/webjars/form-flow/0.0.1/js/honeycrisp.min.js"></script>
<script src="/webjars/form-flow/0.0.1/js/formFlowDropZone.js"></script>
<script src="/webjars/form-flow/0.0.1/js/formFlowLanguageSelector.js"></script>
<script src="/webjars/form-flow/0.0.1/js/formFlowDisableMultipleFormSubmit.js"></script>
</th:block>
</html>
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();
}
}

0 comments on commit d643219

Please sign in to comment.