webmcp
view framework/env/param/get_list.lua @ 330:22275c74023a
Modified error message when setting global variable
| author | jbe | 
|---|---|
| date | Tue Mar 24 13:30:54 2015 +0100 (2015-03-24) | 
| parents | 32ec28229bb5 | 
| children | 209d378c0a3b | 
 line source
     1 --[[--
     2 values =         -- list of values casted to the chosen param_type
     3 param.get_list(
     4   key,           -- name of the parameter without "[]" suffix
     5   param_type,    -- desired type of the returned values
     6 )
     8 Same as param.get(...), but used for parameters which contain a list of values. For external GET/POST parameters the parameter name gets suffixed with "[]".
    10 --]]--
    12 function param.get_list(key, param_type)
    13   local param_type = param_type or atom.string
    14   if param._exchanged then
    15     local values = param._exchanged.params[key] or {}
    16     if type(values) ~= "table" then
    17       error("Parameter has unexpected type.")
    18     end
    19     for idx, value in ipairs(values) do
    20       if not atom.has_type(value, param_type) then
    21         error("Element of parameter list has unexpected type.")
    22       end
    23     end
    24     return values
    25   else
    26     local format_info = request.get_param{ name = key .. "__format" }
    27     local parser = param._get_parser(format_info, param_type)
    28     local raw_values = request.get_param{ name = key .. "[]", multiple = true }
    29     local values = {}
    30     if raw_values then
    31       for idx, value in ipairs(raw_values) do
    32         values[idx] = parser(raw_values[idx])
    33       end
    34     end
    35     return values
    36   end
    37 end
