I've created a custom WiFi configuration page using LuCI Javascript, but validation/saving is being weird.
The ssid field is between 1 and 32 characters, so I use o.datatype = 'rangelength(1,32)';
When I delete ALL characters in the field (so length==0), it is NOT compliant with o.datatype but it still updates the UCI by fully deleting default_radio0.ssid. The ssid field custom o.write function is not called for this empty value.
It looks like the problem is in function parse() in form.js, (roughly line 1983 for version 21.02).
parse() calls this.write() only when fval is not empty, and otherwise deletes it. That means it unfortunately deletes the ssid when there are zero characters in the field.
if (fval != '' && fval != null) {
if (this.forcewrite || !isEqual(cval, fval))
return Promise.resolve(this.write(section_id, fval));
}
else {
if (!active || this.rmempty || this.optional) {
return Promise.resolve(this.remove(section_id));
}
...
}
The behaviour I need instead is:
- Only ever change uci if the field has passed validation
- No automatic delete for empty values
- Always call the o.write function for a validated value (including possibly an empty string)
Any ideas on how to do this?
Code for my Javacript render() function, running in 21.02, is as follows:
render: function () {
var m, s, o;
m = new form.Map('wireless', 'WiFi',
'Choose a WiFi name and a suitable password'
);
s = m.section(form.NamedSection, 'default_radio0');
o = s.option(form.Value, 'ssid', _('WiFi name:'), 'WiFi name goes here');
o.datatype = 'rangelength(1,32)';
o.write = function(section_id, value) {
m.data.set('wireless', 'default_radio0', 'ssid', value);
m.data.set('wireless', 'default_radio1', 'ssid', value);
}
...
return m.render();
}