OpenWrt Forum Archive

Topic: Problem: validate SSID of wifi in LuCI (SOLVED)

The content of this topic has been archived on 16 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I want to validate the ssid . Because if ssid is  void , the wifi will get down. I want to restrain it for not void.  If it is void then I get value from UCI config file. So I wrote code in wifi.lua as follows:

function ssid.write(self,section)
    local val = m:formvalue(self:cbid(section))
    if #val < 1 then 
        val = self:cfgvalue(section)
         m.uci:set("wireless",section,"ssid",val)
    end
end

It doesn't work. Why?

The web data will be parsed in http.lua , then will be written to uci file. But if I add some rules to data (which will be written to uci file) in /model/cbi/*.lua then the data written to uci file will just follow my rules. So I get the data from the web using formvalue()  then add some rules , write to uci file at last. But when  the ssid is void , the wifi will still get down. And my rules just does nothing.

Why? Some wrong with my understanding with the process?

(Last edited by Wandy on 17 Oct 2014, 10:17)

This is odd. Did I got some mistakes in the code? But I can restrain  the length of the ssid, but not the void condition. If the ssid is empty this always gets through the ssid option in uci file will be gone. But I read the code in model/network.lua the ssid is always from the uci file. How I just can't catch the condition of empty ssid? I think empty ssid is just '' , right?

(Last edited by Wandy on 17 Oct 2014, 08:44)

CBI will invoke remove() if the value is empty. The write() callback is only invoked if there is any value.

jow wrote:

CBI will invoke remove() if the value is empty. The write() callback is only invoked if there is any value.

Oh, My......!!!This is really a trick. Thank you. It really a big help !!!! Thank you. I will read it the cbi .

Use function ssid.validate(self, value) then return value if value is valid or return nil, "error text" if invalid. The "error text" is optional.
So shortest form:

function ssid.validate(self, value)
    if not value then return nil
end

If there is no input in the luci form then value is "nil".

Thank you. I figure it out.

just one more line code. Shame on me about the mistake

ssid .rmempty = false

This line will make sure the ssid option will not be deleted.
Then I use the last uci config,  the wifi will not get down.

(Last edited by Wandy on 17 Oct 2014, 10:05)

The discussion might have continued from here.