- md5
- 8d6b3f5cbbdaadf87ac3187c57734ba9
- sha1
- 9b2ac2c3aa761ac94c40fdbbd8fca21f8313d615
- sha256
- cdca8d4650396e8cffa321a6b18cc8b7cde6682d7935e967c9ad7469372957fb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
/**
* PLEASE DO NOT MODIFY THIS FILE. WORK ON THE ES6 VERSION.
* OTHERWISE YOUR CHANGES WILL BE REPLACED ON THE NEXT BUILD.
**/
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
(function (document) {
'use strict';
var JFormValidator = /*#__PURE__*/function () {
function JFormValidator() {
var _this = this;
_classCallCheck(this, JFormValidator);
this.customValidators = {};
this.handlers = [];
this.handlers = {};
this.removeMarking = this.removeMarking.bind(this);
this.inputEmail = function () {
var input = document.createElement('input');
input.setAttribute('type', 'email');
return input.type !== 'text';
}; // Default handlers
this.setHandler('username', function (value) {
var regex = new RegExp('[<|>|"|\'|%|;|(|)|&]', 'i');
return !regex.test(value);
});
this.setHandler('password', function (value) {
var regex = /^\S[\S ]{2,98}\S$/;
return regex.test(value);
});
this.setHandler('numeric', function (value) {
var regex = /^(\d|-)?(\d|,)*\.?\d*$/;
return regex.test(value);
});
this.setHandler('email', function (value) {
var newValue = window.punycode.toASCII(value);
var regex = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
return regex.test(newValue);
}); // Attach all forms with a class 'form-validate'
var forms = [].slice.call(document.querySelectorAll('form'));
forms.forEach(function (form) {
if (form.classList.contains('form-validate')) {
_this.attachToForm(form);
}
});
}
_createClass(JFormValidator, [{
key: "setHandler",
value: function setHandler(name, func, en) {
var isEnabled = en === '' ? true : en;
this.handlers[name] = {
enabled: isEnabled,
exec: func
};
} // eslint-disable-next-line class-methods-use-this
}, {
key: "markValid",
value: function markValid(element) {
// Get a label
var label = element.form.querySelector("label[for=\"".concat(element.id, "\"]"));
var message;
if (element.classList.contains('required') || element.getAttribute('required')) {
if (label) {
message = label.querySelector('span.form-control-feedback');
}
}
element.classList.remove('form-control-danger');
element.classList.remove('invalid');
element.classList.add('form-control-success');
element.parentNode.classList.remove('has-danger');
element.parentNode.classList.add('has-success');
element.setAttribute('aria-invalid', 'false'); // Remove message
if (message) {
message.parentNode.removeChild(message);
} // Restore Label
if (label) {
label.classList.remove('invalid');
}
} // eslint-disable-next-line class-methods-use-this
}, {
key: "markInvalid",
value: function markInvalid(element, empty) {
// Get a label
var label = element.form.querySelector("label[for=\"".concat(element.id, "\"]"));
element.classList.remove('form-control-success');
element.classList.remove('valid');
element.classList.add('form-control-danger');
element.classList.add('invalid');
element.parentNode.classList.remove('has-success');
element.parentNode.classList.add('has-danger');
element.setAttribute('aria-invalid', 'true'); // Display custom message
var mesgCont;
var message = element.getAttribute('data-validation-text');
if (label) {
mesgCont = label.querySelector('span.form-control-feedback');
}
if (!mesgCont) {
var elMsg = document.createElement('span');
elMsg.classList.add('form-control-feedback');
if (empty && empty === 'checkbox') {
elMsg.innerHTML = message || Joomla.JText._('JLIB_FORM_FIELD_REQUIRED_CHECK');
} else if (empty && empty === 'value') {
elMsg.innerHTML = message || Joomla.JText._('JLIB_FORM_FIELD_REQUIRED_VALUE');
} else {
elMsg.innerHTML = message || Joomla.JText._('JLIB_FORM_FIELD_INVALID_VALUE');
}
if (label) {
label.appendChild(elMsg);
}
} // Mark the Label as well
if (label) {
label.classList.add('invalid');
}
} // eslint-disable-next-line class-methods-use-this
}, {
key: "removeMarking",
value: function removeMarking(element) {
// Get the associated label
var message;
var label = element.form.querySelector("label[for=\"".concat(element.id, "\"]"));
if (label) {
message = label.querySelector('span.form-control-feedback');
}
element.classList.remove('form-control-danger');
element.classList.remove('form-control-success');
element.classList.remove('invalid');
element.classList.add('valid');
element.parentNode.classList.remove('has-danger');
element.parentNode.classList.remove('has-success'); // Remove message
if (message) {
if (label) {
label.removeChild(message);
}
} // Restore Label
if (label) {
label.classList.remove('invalid');
}
}
}, {
key: "handleResponse",
value: function handleResponse(state, element, empty) {
// Set the element and its label (if exists) invalid state
if (element.tagName.toLowerCase() !== 'button' && element.value !== undefined) {
if (state === false) {
this.markInvalid(element, empty);
} else {
this.markValid(element);
}
}
}
}, {
key: "validate",
value: function validate(element) {
var tagName; // Ignore the element if its currently disabled,
// because are not submitted for the http-request.
// For those case return always true.
if (element.getAttribute('disabled') === 'disabled' || element.getAttribute('display') === 'none') {
this.handleResponse(true, element);
return true;
} // If the field is required make sure it has a value
if (element.getAttribute('required') || element.classList.contains('required')) {
tagName = element.tagName.toLowerCase();
if (tagName === 'fieldset' && (element.classList.contains('radio') || element.classList.contains('checkboxes'))) {
if (!element.querySelector('input:checked').length) {
this.handleResponse(false, element, 'checkbox');
return false;
}
} else if (element.getAttribute('type') === 'checkbox' && element.checked !== true || tagName === 'select' && !element.value.length) {
this.handleResponse(false, element, 'checkbox');
return false;
} else if (!element.value || element.classList.contains('placeholder')) {
// If element has class placeholder that means it is empty.
this.handleResponse(false, element, 'value');
return false;
}
} // Only validate the field if the validate class is set
var handler = element.getAttribute('class') && element.getAttribute('class').match(/validate-([a-zA-Z0-9_-]+)/) ? element.getAttribute('class').match(/validate-([a-zA-Z0-9_-]+)/)[1] : '';
if (element.getAttribute('pattern') && element.getAttribute('pattern') !== '') {
if (element.value.length) {
var isValid = new RegExp("^".concat(element.getAttribute('pattern'), "$")).test(element.value);
this.handleResponse(isValid, element, 'empty');
return isValid;
}
if (element.hasAttribute('required') || element.classList.contains('required')) {
this.handleResponse(false, element, 'empty');
return false;
}
this.handleResponse(true, element);
return false;
}
if (handler === '') {
this.handleResponse(true, element);
return true;
} // Check the additional validation types
if (handler && handler !== 'none' && this.handlers[handler] && element.value) {
// Execute the validation handler and return result
if (this.handlers[handler].exec(element.value, element) !== true) {
this.handleResponse(false, element, 'invalid_value');
return false;
}
} // Return validation state
this.handleResponse(true, element);
return true;
}
}, {
key: "isValid",
value: function isValid(form) {
var _this2 = this;
var valid = true;
var message;
var error;
var invalid = []; // Validate form fields
var fields = [].slice.call(form.querySelectorAll('input, textarea, select, button'));
fields.forEach(function (field) {
if (_this2.validate(field) === false) {
valid = false;
invalid.push(field);
}
}); // Run custom form validators if present
if (Object.keys(this.customValidators).length) {
Object.keys(this.customValidators).foreach(function (key) {
if (_this2.customValidators[key].exec() !== true) {
valid = false;
}
});
}
if (!valid && invalid.length > 0) {
if (form.getAttribute('data-validation-text')) {
message = form.getAttribute('data-validation-text');
} else {
message = Joomla.JText._('JLIB_FORM_CONTAINS_INVALID_FIELDS');
}
error = {
error: [message]
};
Joomla.renderMessages(error);
}
return valid;
}
}, {
key: "attachToForm",
value: function attachToForm(form) {
var _this3 = this;
var inputFields = [];
var elements = [].slice.call(form.querySelectorAll('input, textarea, select, button, fieldset')); // Iterate through the form object and attach the validate method to all input fields.
elements.forEach(function (element) {
var tagName = element.tagName.toLowerCase();
if (['input', 'textarea', 'select', 'fieldset'].indexOf(tagName) > -1 && element.classList.contains('required')) {
element.setAttribute('required', '');
} // Attach isValid method to submit button
if ((tagName === 'input' || tagName === 'button') && (element.getAttribute('type') === 'submit' || element.getAttribute('type') === 'image')) {
if (element.classList.contains('validate')) {
element.addEventListener('click', function () {
return _this3.isValid(form);
});
}
} else if (tagName !== 'button' && !(tagName === 'input' && element.getAttribute('type') === 'button')) {
// Attach validate method only to fields
if (tagName !== 'fieldset') {
element.addEventListener('blur', function (_ref) {
var target = _ref.target;
return _this3.validate(target);
});
element.addEventListener('focus', function (_ref2) {
var target = _ref2.target;
return _this3.removeMarking(target);
});
if (element.classList.contains('validate-email') && _this3.inputEmail) {
element.setAttribute('type', 'email');
}
}
inputFields.push(element);
}
});
}
}, {
key: "custom",
get: function get() {
return this.customValidators;
},
set: function set(value) {
this.customValidators = value;
}
}]);
return JFormValidator;
}();
var initialize = function initialize() {
document.formvalidator = new JFormValidator(); // Cleanup
document.removeEventListener('DOMContentLoaded', initialize);
};
document.addEventListener('DOMContentLoaded', initialize);
})(document);
The same content is in 4 releases, Joomla 4.0.0-beta to 4.0.0-beta4. See them all, or every version of this file.