Problem saving unvalidated fields in Luci

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:

  1. Only ever change uci if the field has passed validation
  2. No automatic delete for empty values
  3. 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();
        }
  1. Fields default to .rmempty = true which makes them optional. Validation happens after optional checking. Try setting o.rmempty = false to fail validation on empty SSID
  2. Should be solved by 1)
  3. Should be possible by setting o.remove = o.write
1 Like

For the ssid I said rmempty = false. And for the WiFi key I did a custom remove function which does the right thing.

Thank you so much for your help!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.