[luci] Please help with validating unique vlaues
Chris Martin
chris at martin.cc
Wed Feb 24 02:16:38 CET 2010
On Tue, Feb 23, 2010 at 12:59 AM, Jo-Philipp Wich wrote:
>
> Hi.
>
>> I have a requirement to validate some input data to make sure that it
>> is unique, unfortunately I don't seem to be able to see a way forward.
>
> You can define a validate function for a whole section, this allows you
> to validate all fields within it. It gets self and the section id as
> parameters.
>
> The fields are stored in a key->value table within self.fields, you can
> access their :formvalue() member function to obtain the values you want.
>
> Try something like that:
>
> m = Map(...)
>
> s = m:section(...)
>
> function s.validate(self, sectionid)
> local field, obj
> local values = { }
>
> for field, obj in pairs(self.fields) do
> local fieldval = obj:formvalue(sectionid)
> if not values[fieldval] then
> values[fieldval] = true
> else
> return nil -- raise error
> end
> end
>
> return sectionid -- signalize success
> end
>
>
> ~ JoW
Jo-Philipp
Thank you for you assistance. It sheds a lot of light in what I need
to do, and how to do it
My layout is a bit more complicated (maybe un-necessarily so). Here
is a link to a screenshot to illustrate.
http://www.martin.cc/Screenshot-NS40%20-%20VLANs%20-%20LuCI.png
I have 4 sections due to the following constraints
(1) The "management" VLAN should not be able to be disabled.
(2) The "data", "voice" & "management" VLANs must not be able to be removed.
So to do the validation, I think that I will need to add a
"validation" function to the map, rather than the section. I have
read through the cbi.lua source and I don't see any support for
validation on "map". I will need to add it myself or find an
alternative way to render the display in a single section.
The map validation function would have to do something like:
enumerate the sections
function s.validate(...)
local values = { }
enumerate the sections
if section.type = vlan
enumerate the options
if option.type = vlan_id
if not values[option.value] then
values[option.vlaue] = true
else
return nil -- error
end
end
done
end
done
return true -- success
end
PS. The VLAN IDs for the management, data & voice VLANs are
currently fixed, just so that I don't have to deal with the changes
associated with device name ie: eth0.id. Once I have modified all the
OpenWrt scripts, they will be able to be changed.
cheers
----------------------------------------------------------
Chris Martin
m: 0419812371
----------------------------------------------------------
More information about the luci
mailing list