-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved error message and changed how error message(s) are displayed in ui
- Loading branch information
Carine Dengler
committed
Oct 5, 2020
1 parent
399e845
commit fd6f122
Showing
2 changed files
with
49 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
{% macro validateLength(textArea, index, length) %} | ||
<script type="text/javascript"> | ||
document.getElementById("form").addEventListener("submit", (evt => validateLength(evt, "{{textArea}}", {{index}}, {{length}}))); | ||
function validateLength(evt, textArea, lengths){ | ||
textAreaElement = document.getElementById(textArea) | ||
var text = textAreaElement.value; | ||
lines = text.split(/\r\n|\n\r|\r|\n/); | ||
fields = lines.map((x) => x.split(/\t+/)); | ||
for (const index in lengths) { | ||
length = lengths[index]; | ||
var validatedField = fields[0][index]; | ||
for (let i=1; i < fields.length; i++){ | ||
fieldValue = fields[i][index] | ||
if (fieldValue.length > length){ | ||
evt.preventDefault(); | ||
startPos = text.search(new RegExp(fieldValue)); | ||
textAreaElement.setSelectionRange(startPos, startPos + fieldValue.length) | ||
textAreaElement.focus(); | ||
var div = document.createElement("div"); | ||
div.className = "alert alert-danger mt-3 mb-3"; | ||
div.textContent = `column '${validatedField}': '${fieldValue}' is too long (maximum ${length} characters)`; | ||
var formElement = document.getElementById("form"); | ||
formElement.insertBefore(div, formElement.firstChild); | ||
document.getElementById("form").addEventListener("submit", (evt => validateLength(evt, "{{textArea}}", {{index}}, {{length}}))); | ||
|
||
function insertValidateLengthError(validatedField, fieldValue, length, siblingId) { | ||
// create unique ID for each error message | ||
validateLengthErrorId = `validateLengthError${Date.now()}`; | ||
var validateLengthError = document.createElement("div"); | ||
validateLengthError.id = validateLengthErrorId; | ||
validateLengthError.className = "alert alert-danger mt-3 mb-3"; | ||
validateLengthError.textContent = `column '${validatedField}': '${fieldValue}' is too long (maximum ${length} characters)`; | ||
if (!siblingId) { | ||
document.getElementById("tokensLabel").insertBefore(validateLengthError, parent.firstChild); | ||
} | ||
else { | ||
document.getElementById(siblingId).after(validateLengthError); | ||
} | ||
return validateLengthErrorId; | ||
}; | ||
|
||
function clearError(tokensLabel){ | ||
newTokensLabel = tokensLabel.cloneNode(false); | ||
tokensLabel.parentNode.replaceChild(newTokensLabel, tokensLabel) | ||
}; | ||
|
||
function validateLength(evt, textAreaId, lengths){ | ||
// clear previous error messages | ||
clearError(document.getElementById("tokensLabel")) | ||
textArea = document.getElementById(textAreaId) | ||
var text = textArea.value; | ||
lines = text.split(/\r\n|\n\r|\r|\n/); | ||
fields = lines.map((x) => x.split(/\t+/)); | ||
for (const index in lengths) { | ||
length = lengths[index]; | ||
var validatedField = fields[0][index]; | ||
var siblingId = ""; | ||
var highlighted = false; | ||
for (let i=1; i < fields.length; i++){ | ||
fieldValue = fields[i][index] | ||
if (fieldValue.length > length){ | ||
siblingId = insertValidateLengthError(validatedField, fieldValue, length, siblingId); | ||
evt.preventDefault(); | ||
// highlight first erroneous line | ||
if (!highlighted) { | ||
startPos = text.search(new RegExp(fieldValue)); | ||
textArea.setSelectionRange(startPos, startPos + fieldValue.length) | ||
textArea.focus(); | ||
highlighted = true; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
</script> | ||
{% endmacro %} |
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