validation
Source: validation.js:9
The LuCI validation class provides functions to perform validation on user input within various form input fields.
To import the class, use 'require validation'. To import it in external JavaScript, use L.require("validation").then(...).
Note: it is not required to import this class in forms for use: it is imported by ui where form elements are defined.
A typical validation is instantiated by first constructing a LuCI.form element and by adding a datatype to the element properties.
Example
'use strict';
...
let m, s, o;
...
o = s.option(form.Value, 'some_value', 'A value element');
o.datatype = 'ipaddr';
...A validator stub can be instantiated so:
const stubValidator = {
factory: validation,
apply: function(type, value, args) {
if (value != null)
this.value = value;
return validation.types[type].apply(this, args);
},
assert: function(condition) {
return !!condition;
}
};and later used so in a custom o.validate function:
...
stubValidator.apply('ipaddr', m4 ? m4[1] : m6[1])
...One can also add validators to HTML UI elements via LuCI.ui#addValidator:
...
s.renderSectionAdd = function(extra_class) {
var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
nameEl = el.querySelector('.cbi-section-create-name');
ui.addValidator(nameEl, 'uciname', true, function(v) {
let sections = [
...uci.sections('config', 'section_type1'),
...uci.sections('config', 'section_type2'),
];
if (sections.find(function(s) {
return s['.name'] == v;
})) {
return _('This may not share the same name as other section type1 or section type2.');
}
if (v.length > 15) return _('Name length shall not exceed 15 characters');
return true;
}, 'blur', 'keyup');
return el;
};
...Static Methods
bytelen(x: string): number
Return byte length of a string using Blob (UTF-8 byte count).
Parameters
x(string) — Input string.
Returns
number— Byte length of the string.
arrayle(a: Array.<number>, b: Array.<number>): boolean
Compare two arrays element-wise: return true if a < b in lexicographic element comparison.
Parameters
a(Array.<number>) — First array.b(Array.<number>) — Second array.
Returns
boolean— True if arrays compare asa < b, false otherwise.
Other
Validator
Parameters
field(string) — the UI field to validate.type(string) — type of validator.optional(boolean) — set the validation result as optional.function(vfunc) — validation function.validatorFactory(ValidatorFactory) — a ValidatorFactory instance.
Returns
Validator— a Validator instance.
ValidatorFactory
Factory to create Validator instances and compile validation expressions.