From 9d7ff647d2e2cee70bee7a7aea7d7b2acf748f9f Mon Sep 17 00:00:00 2001 From: MattMo Date: Sat, 22 Apr 2023 11:59:19 -0700 Subject: [PATCH] Added a validate file option and included documentation. --- ignite-html-validate.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ignite-html-validate.js b/ignite-html-validate.js index 7d21ec5..a126fe6 100644 --- a/ignite-html-validate.js +++ b/ignite-html-validate.js @@ -173,6 +173,8 @@ IgniteTemplate.prototype.validate = function (callback) { value = this.element.checked; } else if (type == "number") { value = Number(this.element.value); + } else if (type == "file") { + value = this.element.files; } else if (this.element.hasAttribute("contenteditable") && this.element.getAttribute("contenteditable").toLowerCase().trim() == "true") { value = this.element.textContent; } else { @@ -327,6 +329,19 @@ IgniteTemplate.prototype.validateMatches = function (compare, msg) { }); } +/** + * Validates an input to make sure it contains at least one file file. + * @param {string|Function|IgniteProperty} msg The message to display when a file has not been selected. + * @returns {IgniteTemplate} This ignite template. + */ +IgniteTemplate.prototype.validateFile = function(msg) { + return this.validate((value, error) => { + if (!value || value.length == 0) { + return error(msg ? msg : 'Must select a file to upload.'); + } + }); +} + /** * Validates this element and all the children within the element. * @returns {Boolean} Whether or not this element and all children in this element are valid.