webmcp
view framework/env/ui/form.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
author | jbe/bsw |
---|---|
date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) |
parents | 9fdfb27f8e67 |
children | d76a8857ba62 |
line source
1 --[[--
2 ui.form{
3 record = record, -- optional record to be used
4 read_only = read_only, -- set to true, if form should be read-only (no submit button)
5 external = external, -- external URL to be used as HTML form action
6 module = module, -- module name to be used for HTML form action
7 view = view, -- view name to be used for HTML form action
8 action = action, -- action name to be used for HTML form action
9 routing = {
10 default = { -- default routing for called action
11 mode = mode, -- "forward" or "redirect"
12 module = module, -- optional module name, defaults to current module
13 view = view, -- view name
14 id = id, -- optional id to be passed to the view
15 params = params -- optional params to be passed to the view
16 },
17 ok = { ... }, -- routing when "ok" is returned by the called action
18 error = { ... }, -- routing when "error" is returned by the called action
19 ... = { ... } -- routing when "..." is returned by the called action
20 }
21 content = function()
22 ... -- code creating the contents of the form
23 end
24 }
26 This functions creates a web form, which encloses the content created by the given 'content' function. When a 'record' is given, ui.field.* helper functions will be able to automatically determine field values by using the given record. If 'read_only' is set to true, then a call of ui.submit{...} will be ignored, and ui.field.* helper functions will behave differently.
28 --]]--
30 function ui.form(args)
31 local args = args or {}
32 local slot_state = slot.get_state_table()
33 local old_record = slot_state.form_record
34 local old_readonly = slot_state.form_readonly
35 slot_state.form_record = args.record
36 if args.readonly then
37 slot_state.form_readonly = true
38 ui.container{ attr = args.attr, content = args.content }
39 else
40 slot_state.form_readonly = false
41 local params = table.new(args.params)
42 local routing_default_given = false
43 if args.routing then
44 for status, settings in pairs(args.routing) do
45 if status == "default" then
46 routing_default_given = true
47 end
48 local module = settings.module or args.module or request.get_module()
49 assert(settings.mode, "No mode specified in routing entry.")
50 assert(settings.view, "No view specified in routing entry.")
51 params["_webmcp_routing." .. status .. ".mode"] = settings.mode
52 params["_webmcp_routing." .. status .. ".module"] = module
53 params["_webmcp_routing." .. status .. ".view"] = settings.view
54 params["_webmcp_routing." .. status .. ".id"] = settings.id
55 if settings.params then
56 for key, value in pairs(settings.params) do
57 params["_webmcp_routing." .. status .. ".params." .. key] = value
58 end
59 end
60 end
61 end
62 if not routing_default_given then
63 params["_webmcp_routing.default.mode"] = "forward"
64 params["_webmcp_routing.default.module"] = request.get_module()
65 params["_webmcp_routing.default.view"] = request.get_view()
66 end
67 params._webmcp_csrf_secret = request.get_csrf_secret()
68 local attr = table.new(args.attr)
69 attr.action = encode.url{
70 external = args.external,
71 module = args.module or request.get_module(),
72 view = args.view,
73 action = args.action,
74 }
75 attr.method = args.method and string.upper(args.method) or "POST"
76 if slot_state.form_opened then
77 error("Cannot open a non-readonly form inside a non-readonly form.")
78 end
79 slot_state.form_opened = true
80 ui.tag {
81 tag = "form",
82 attr = attr,
83 content = function()
84 if args.id then
85 ui.hidden_field{ name = "_webmcp_id", value = args.id }
86 end
87 for key, value in pairs(params) do
88 ui.hidden_field{ name = key, value = value }
89 end
90 if args.content then
91 args.content()
92 end
93 end
94 }
95 slot_state.form_opened = false
96 end
97 slot_state.form_readonly = old_readonly
98 slot_state.form_record = old_record
99 end