-
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.
feat: replace HTML validation attrs with soft validation
We mustn't prevent form submission, but we still want to give some feedback to the user. Closes #57
- Loading branch information
Showing
13 changed files
with
279 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,68 @@ | ||
/* | ||
* This file is part of the QuestionPy Moodle plugin - https://questionpy.org | ||
* | ||
* Moodle is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Moodle is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* Softly (i.e. without preventing form submission) validates required and/or pattern conditions on the given element. | ||
* | ||
* @param {HTMLInputElement} element | ||
*/ | ||
function check_conditions(element) { | ||
if (element.dataset.qpy_required !== undefined) { | ||
if (element.value === null || element.value === "") { | ||
element.classList.add("qpy-invalid"); | ||
return; | ||
} | ||
} | ||
|
||
const pattern = element.dataset.qpy_pattern; | ||
if (pattern !== undefined && element.value !== null && element.value !== "" | ||
&& !element.value.match(`^(?:${pattern})$`)) { | ||
element.classList.add("qpy-invalid"); | ||
element.setAttribute("aria-invalid", "true"); | ||
return; | ||
} | ||
|
||
const minLength = element.dataset.qpy_minlength; | ||
if (minLength !== undefined && element.value !== null && element.value !== "" | ||
&& element.value.length < parseInt(minLength)) { | ||
element.classList.add("qpy-invalid"); | ||
element.setAttribute("aria-invalid", "true"); | ||
return; | ||
} | ||
|
||
const maxLength = element.dataset.qpy_maxlength; | ||
if (maxLength !== undefined && element.value !== null && element.value !== "" | ||
&& element.value.length > parseInt(maxLength)) { | ||
element.classList.add("qpy-invalid"); | ||
element.setAttribute("aria-invalid", "true"); | ||
return; | ||
} | ||
|
||
element.classList.remove("qpy-invalid"); | ||
element.removeAttribute("aria-invalid"); | ||
} | ||
|
||
/** | ||
* Adds change event handlers for soft validation. | ||
*/ | ||
export function init() { | ||
document.querySelectorAll("[data-qpy_required], [data-qpy_pattern], [data-qpy_minlength], [data-qpy_maxlength]") | ||
.forEach(element => { | ||
check_conditions(element); | ||
element.addEventListener("change", event => check_conditions(event.target)); | ||
}); | ||
} |
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
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 |
---|---|---|
|
@@ -73,3 +73,7 @@ | |
.qpy-repetition-remove { | ||
margin: .5em; | ||
} | ||
|
||
.qpy-invalid { | ||
border-color: red; | ||
} |
Oops, something went wrong.