[luci] CBI: Validate a field based on the value of another field?

Luca Olivetti luca at ventoso.org
Thu Jul 31 13:34:25 CEST 2014


Hello, I couldn't find any documentation on how to validate a field in a
cbi model based on the value of another field.
There's a seemingly outdated one on creating a section.validate
function, but that's called *before* rendering and not to validate a *post*.

Specifically I want to check that a "max" value is greater than a "min"
value. Failing to find a suitable "datatype" incantation (is there one
to do what I want?), I tried something like this:

rtp_first=s:option(Value,"rtp_port_first", translate("First rtp port"),
translate("Voip will try to bind rtp ports starting from this one."))
rtp_first.optional=false
rtp_first.rmempty=false
rtp_first.datatype="port"

rtp_last=s:option(Value,"rtp_port_last", translate("Last rtp port"),
translate("This is the last rtp port voip will try to use."))
rtp_last.optional=false
rtp_last.rmempty=false
rtp_last.datatype="port"
function rtp_last.validate(self,value)
  f=self.section.fields.rtp_port_first
  rtp_first=f:formvalue(f:cbid('main')) <--
  rtp_last=value
  if rtp_first and rtp_last then
    if tonumber(rtp_last)<tonumber(rtp_first)  then
      return nil,translate("last rtp port must be >= first rtp port")
    end
  end
  return value
end


the problem is, this is an anonymous section, so I didn't find a way to
obtain the auto generated section name in the line marked with "<--".

Any suggestion (apart from changing the logic to specify first port,
number of ports)?
TIA

Bye
-- 
Luca


More information about the luci mailing list