This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
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.
store validation errors between request in session object and remove JS
... ...
- Loading branch information
Connor O'Shaughnessy
committed
Dec 5, 2023
1 parent
8099734
commit e54d1aa
Showing
5 changed files
with
89 additions
and
114 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,78 +1 @@ | ||
// Your custom JS goes here... | ||
|
||
const noFileError = ( | ||
'<p class="govuk-error-message">\n' + | ||
' Some of your data is not what we expect, so we could not run the full checks.\n' + | ||
'</p>\n' + | ||
'<p class="govuk-error-message">\n' + | ||
' Fix these problems and re-upload:\n' + | ||
'</p>\n' + | ||
'<ul>\n' + | ||
' <li>Select your returns template.</li>' + | ||
'</ul>\n') | ||
|
||
const noFileSummary = ( | ||
'<div role="alert">\n' + | ||
' <h2 class="govuk-error-summary__title">There is a problem</h2>\n' + | ||
' <div class="govuk-error-summary__body govuk-error-message">\n' + | ||
' <p class="govuk-error-message">\n' + | ||
' Some of your data is not what we expect, so we could not run the full checks.\n' + | ||
' </p>\n' + | ||
' <p class="govuk-error-message">\n' + | ||
' Fix these problems and re-upload:\n' + | ||
' </p>\n' + | ||
' <ul>\n' + | ||
' <li><a class="govuk-error-message" href="#ingest_spreadsheet">Select your returns template.</a></li>\n' + | ||
' </ul>\n' + | ||
' </div>\n' + | ||
'</div>\n') | ||
|
||
|
||
document.addEventListener('DOMContentLoaded', function() { | ||
"This event listener checks to see if a user submits an empty form and updates the DOM to display error messages. " + | ||
"It is writen in such a way that if correct html classes cannot be found on the page the behaviour will default " + | ||
"to standard error messageing e.g a new request will be made."; | ||
// Find the form by its ID | ||
var form = document.getElementById('submit_form'); | ||
|
||
// Add a submit event listener to the form | ||
form.addEventListener('submit', function(event) { | ||
// Get the value of the file input by its ID | ||
var fileInput = document.getElementById('ingest_spreadsheet'); | ||
|
||
// Check if the file input is empty | ||
if (fileInput.files.length === 0) { | ||
fileInput.classList.add('govuk-file-upload--error'); | ||
|
||
var errorMessage = document.getElementById('error_message'); | ||
var errorSummary = document.getElementById('error_summary'); | ||
if (errorMessage && errorSummary) { | ||
// If the error message and summary already exists, update its content | ||
errorMessage.innerHTML = noFileError | ||
errorSummary.innerHTML = noFileSummary | ||
} else if (!errorMessage && !errorSummary){ | ||
// Create the error message and summary elements if they don't exist | ||
errorMessage = document.createElement('p'); | ||
errorMessage.id = 'error_message'; | ||
errorMessage.className = 'govuk-error-message'; | ||
errorMessage.innerHTML = noFileError | ||
|
||
errorSummary = document.createElement('div'); | ||
errorSummary.id = 'error_summary'; | ||
errorSummary.className = 'govuk-error-summary' | ||
errorSummary.innerHTML = noFileSummary | ||
|
||
// Insert into the DOM | ||
var errorDiv = document.querySelector('.govuk-form-group'); | ||
errorDiv.classList.add('govuk-form-group--error'); | ||
errorDiv.prepend(errorMessage) | ||
|
||
var container = document.getElementById('main_container'); | ||
container.prepend(errorSummary) | ||
} else { | ||
return | ||
} | ||
event.preventDefault(); | ||
} | ||
}); | ||
}); |
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
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