mirror of
https://github.com/openwrt/luci.git
synced 2026-02-04 12:06:01 +08:00
While it's possible to stack validation using .datatype semantics,
e.g. o.datatype = 'and(foo,bar)' and those execute serially, they
depend on the internal validation factory and the built-ins there.
Now, one can define multiple functions in the calling code (passed
in an array) which execute serially. All validation functions
shall return true to succeed.
e.g.
```js
function foo(sid, val) {
return val.includes('foo');
}
function bar(sid, val) {
return val.includes('bar');
}
o = s.option(form.Value, 'foobar', _('foobar'));
o.default = 'foobar';
o.validate = [foo, bar];
```
This helps make validation less complex when special data-types
with high cardinality are in play. Previously, reuse of the this
context when calling sub functions was also lost.
The validate property passed to the new ui widget in form.js
```js
new ui.XX(..., { ... validate: this.getValidator(section_id), ...});
```
takes this.getValidator, which either binds a single validation
function or calls all of the validators, if the form element's
this.validate was an [array]. The result and effect are the same.
If a ui element is manually instantiated (when it's not being
called via form.js and this.getValidator might be unavailable),
passing an array to options.validate works identically:
```js
function foo(val) {
return val.includes('foo');
}
function bar(val) {
return val.includes('bar');
}
new ui.XX(..., { ... validate: [foo, bar], ...});
```
And the validation factory calls them serially.
Signed-off-by: Paul Donald <newtwen+github@gmail.com>