-
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
339 additions
and
36 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,157 @@ | ||
/* | ||
* 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/>. | ||
*/ | ||
|
||
import $ from "jquery"; | ||
import "theme_boost/bootstrap/popover"; | ||
import {get_string as getString} from "core/str"; | ||
|
||
/** | ||
* If the given input(-like) element is labelled, returns the label element. Returns null otherwise. | ||
* | ||
* @param {HTMLElement} input | ||
* @return {HTMLLabelElement | null} | ||
* @see {@link https://html.spec.whatwg.org/multipage/forms.html#the-label-element} | ||
*/ | ||
function getLabelFor(input) { | ||
// A label can reference its labeled control in its for attribute. | ||
const id = input.id; | ||
if (id !== "") { | ||
const label = document.querySelector(`label[for='${id}']`); | ||
if (label) { | ||
return label; | ||
} | ||
} | ||
|
||
// Or the labeled control can be a descendant of the label. | ||
const label = input.closest("label"); | ||
if (label) { | ||
return label; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Marks the given input element as invalid. | ||
* | ||
* @param {HTMLElement} element | ||
* @param {string} message validation message to show | ||
* @param {boolean} ariaInvalid | ||
*/ | ||
function markInvalid(element, message, ariaInvalid = true) { | ||
element.classList.add("is-invalid"); | ||
if (ariaInvalid) { | ||
element.setAttribute("aria-invalid", "true"); | ||
} else { | ||
element.removeAttribute("aria-invalid"); | ||
} | ||
|
||
let popoverTarget = element; | ||
if (element.type === "checkbox" || element.type === "radio") { | ||
// Checkboxes and radios make for a very small hit area for the popover, so we attach the popover to the label. | ||
const label = getLabelFor(element); | ||
if (!label) { | ||
// No label -> Add the popover just to the checkbox. | ||
popoverTarget = element; | ||
} else if (label.contains(element)) { | ||
// Label contains checkbox -> Add the popover just to the label. | ||
popoverTarget = label; | ||
} else { | ||
// Separate label and checkbox -> Add the popover to both. | ||
popoverTarget = [element, label]; | ||
} | ||
} | ||
|
||
$(popoverTarget).popover({ | ||
toggle: "popover", | ||
trigger: "hover", | ||
content: message | ||
}); | ||
} | ||
|
||
/** | ||
* Undoes what {@link markInvalid} did. | ||
* | ||
* @param {HTMLInputElement} element | ||
*/ | ||
function unmarkInvalid(element) { | ||
element.classList.remove("is-invalid"); | ||
element.removeAttribute("aria-invalid"); | ||
|
||
$([element, getLabelFor(element)]).popover("dispose"); | ||
} | ||
|
||
/** | ||
* Softly (i.e. without preventing form submission) validates constraints on the given element. | ||
* | ||
* @param {HTMLInputElement} element | ||
*/ | ||
async function checkConditions(element) { | ||
if (element.dataset.qpy_required !== undefined) { | ||
let isPresent; | ||
if (element.type === "checkbox" || element.type === "radio") { | ||
isPresent = element.checked; | ||
} else { | ||
isPresent = element.value !== null && element.value !== ""; | ||
} | ||
|
||
if (!isPresent) { | ||
const message = await getString("input_missing", "qtype_questionpy"); | ||
// Aria-invalid shouldn't be set for missing inputs until the user has tried to submit them. | ||
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-invalid | ||
markInvalid(element, message, false); | ||
return; | ||
} | ||
} | ||
|
||
const pattern = element.dataset.qpy_pattern; | ||
if (pattern !== undefined && element.value !== null && element.value !== "" | ||
&& !element.value.match(`^(?:${pattern})$`)) { | ||
const message = await getString("input_invalid", "qtype_questionpy"); | ||
markInvalid(element, message); | ||
return; | ||
} | ||
|
||
const minLength = element.dataset.qpy_minlength; | ||
if (minLength !== undefined && element.value !== null && element.value !== "" | ||
&& element.value.length < parseInt(minLength)) { | ||
const message = await getString("input_too_short", "qtype_questionpy", minLength); | ||
markInvalid(element, message); | ||
return; | ||
} | ||
|
||
const maxLength = element.dataset.qpy_maxlength; | ||
if (maxLength !== undefined && element.value !== null && element.value !== "" | ||
&& element.value.length > parseInt(maxLength)) { | ||
const message = await getString("input_too_long", "qtype_questionpy", maxLength); | ||
markInvalid(element, message); | ||
return; | ||
} | ||
|
||
unmarkInvalid(element); | ||
} | ||
|
||
/** | ||
* Adds change event handlers for soft validation. | ||
*/ | ||
export async function init() { | ||
for (const element of document | ||
.querySelectorAll("[data-qpy_required], [data-qpy_pattern], [data-qpy_minlength], [data-qpy_maxlength]")) { | ||
await checkConditions(element); | ||
element.addEventListener("change", event => checkConditions(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
Oops, something went wrong.