* Splitted up value validation code from ffluci.http.formvalue to ffluci.util.validate
This commit is contained in:
parent
f989f6ee00
commit
e8b87fffb9
2 changed files with 32 additions and 34 deletions
|
@ -54,43 +54,18 @@ function request_redirect(category, module, action)
|
||||||
redirect(pattern:format(category, module, action))
|
redirect(pattern:format(category, module, action))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Form validation function:
|
-- Gets form value from key
|
||||||
-- Gets a form variable "key".
|
function formvalue(key, default)
|
||||||
-- If it does not exist: return "default"
|
local c = formvalues()
|
||||||
-- If cast_number is true and "key" is not a number: return "default"
|
|
||||||
-- If valid is a table and "key" is not in it: return "default"
|
|
||||||
-- If valid is a function and returns nil: return "default"
|
|
||||||
-- Else return the value of "key"
|
|
||||||
--
|
|
||||||
-- Examples:
|
|
||||||
-- Get a form variable "foo" and return "bar" if it is not set
|
|
||||||
-- = formvalue("foo", "bar")
|
|
||||||
--
|
|
||||||
-- Get "foo" and make sure it is either "bar" or "baz"
|
|
||||||
-- = formvalue("foo", nil, nil, {"bar", "baz"})
|
|
||||||
--
|
|
||||||
-- Get "foo", make sure its a number and below 10 else return 5
|
|
||||||
-- = formvalue("foo", 5, true, function(a) return a < 10 and a or nil end)
|
|
||||||
function formvalue(key, default, cast_number, valid, table)
|
|
||||||
table = table or formvalues()
|
|
||||||
|
|
||||||
if table[key] == nil then
|
for match in key:gmatch("%w+") do
|
||||||
return default
|
c = c[match]
|
||||||
else
|
if c == nil then
|
||||||
local value = table[key]
|
return default
|
||||||
|
|
||||||
value = cast_number and tonumber(value) or not cast_number and nil
|
|
||||||
|
|
||||||
if type(valid) == "function" then
|
|
||||||
value = valid(value)
|
|
||||||
elseif type(valid) == "table" then
|
|
||||||
if not ffluci.util.contains(valid, value) then
|
|
||||||
value = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return value or default
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,29 @@ function updfenv(f, extscope)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Validates a variable
|
||||||
|
function validate(value, cast_number, cast_int, valid)
|
||||||
|
if cast_number or cast_int then
|
||||||
|
value = tonumber(value)
|
||||||
|
end
|
||||||
|
|
||||||
|
if cast_int and not(value % 1 == 0) then
|
||||||
|
value = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
if type(valid) == "function" then
|
||||||
|
value = valid(value)
|
||||||
|
elseif type(valid) == "table" then
|
||||||
|
if not ffluci.util.contains(valid, value) then
|
||||||
|
value = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Returns the filename of the calling script
|
-- Returns the filename of the calling script
|
||||||
function __file__()
|
function __file__()
|
||||||
return debug.getinfo(2, 'S').source:sub(2)
|
return debug.getinfo(2, 'S').source:sub(2)
|
||||||
|
|
Loading…
Reference in a new issue