webmcp

view framework/env/ui/form.lua @ 562:328f120924a2

Removed if-clause when initializing file descriptor set to avoid compiler warning for mondelefant_conn_try_wait
author jbe
date Fri Feb 05 15:51:39 2021 +0100 (2021-02-05)
parents 32ec28229bb5
children
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 file_upload = file_upload, -- must be set to true, if form contains file upload element
6 external = external, -- external URL to be used as HTML form action
7 module = module, -- module name to be used for HTML form action
8 view = view, -- view name to be used for HTML form action
9 action = action, -- action name to be used for HTML form action
10 routing = {
11 default = { -- default routing for called action
12 mode = mode, -- "forward" or "redirect"
13 module = module, -- optional module name, defaults to current module
14 view = view, -- view name
15 id = id, -- optional id to be passed to the view
16 params = params, -- optional params to be passed to the view
17 anchor = anchor -- optional anchor for URL
18 },
19 ok = { ... }, -- routing when "ok" is returned by the called action
20 error = { ... }, -- routing when "error" is returned by the called action
21 ... = { ... } -- routing when "..." is returned by the called action
22 },
23 content = function()
24 ... -- code creating the contents of the form
25 end
26 }
28 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.
30 --]]--
32 local function prepare_routing_params(params, routing, default_module)
33 local routing_default_given = false
34 if routing then
35 for status, settings in pairs(routing) do
36 if status == "default" then
37 routing_default_given = true
38 end
39 local module = settings.module or default_module or request.get_module()
40 assert(settings.mode, "No mode specified in routing entry.")
41 assert(settings.view, "No view specified in routing entry.")
42 params["_webmcp_routing." .. status .. ".mode"] = settings.mode
43 params["_webmcp_routing." .. status .. ".module"] = module
44 params["_webmcp_routing." .. status .. ".view"] = settings.view
45 params["_webmcp_routing." .. status .. ".id"] = settings.id
46 params["_webmcp_routing." .. status .. ".anchor"] = settings.anchor
47 if settings.params then
48 for key, value in pairs(settings.params) do
49 params["_webmcp_routing." .. status .. ".params." .. key] = value
50 end
51 end
52 end
53 end
54 if not routing_default_given then
55 params["_webmcp_routing.default.mode"] = "forward"
56 params["_webmcp_routing.default.module"] = request.get_module()
57 params["_webmcp_routing.default.view"] = request.get_view()
58 end
59 return params
60 end
62 function ui.form(args)
63 local args = args or {}
64 local slot_state = slot.get_state_table()
65 local old_record = slot_state.form_record
66 local old_readonly = slot_state.form_readonly
67 local old_file_upload = slot_state.form_file_upload
68 slot_state.form_record = args.record
69 if args.readonly then
70 slot_state.form_readonly = true
71 ui.container{ attr = args.attr, content = args.content }
72 else
73 slot_state.form_readonly = false
74 local params = table.new(args.params)
75 prepare_routing_params(params, args.routing, args.module)
76 params._webmcp_csrf_secret = request.get_csrf_secret()
77 local attr = table.new(args.attr)
78 if attr.enctype=="multipart/form-data" or args.file_upload then
79 slot_state.form_file_upload = true
80 if attr.enctype == nil then
81 attr.enctype = "multipart/form-data"
82 end
83 end
84 attr.action = encode.url{
85 external = args.external,
86 module = args.module or request.get_module(),
87 view = args.view,
88 action = args.action,
89 }
90 attr.method = args.method and string.upper(args.method) or "POST"
91 if slot_state.form_opened then
92 error("Cannot open a non-readonly form inside a non-readonly form.")
93 end
94 slot_state.form_opened = true
95 ui.tag {
96 tag = "form",
97 attr = attr,
98 content = function()
99 if args.id then
100 ui.hidden_field{ name = "_webmcp_id", value = args.id }
101 end
102 for key, value in pairs(params) do
103 ui.hidden_field{ name = key, value = value }
104 end
105 if args.content then
106 args.content()
107 end
108 end
109 }
110 slot_state.form_opened = false
111 end
112 slot_state.form_file_upload = old_file_upload
113 slot_state.form_readonly = old_readonly
114 slot_state.form_record = old_record
115 end

Impressum / About Us