# HG changeset patch # User jbe # Date 1324242598 -3600 # Node ID 36ebcae1cde328c936abb69d0a3cc2b6c8edc9fb # Parent 594a85118cb7338f2c5f12f253de56ec98a6cb6c File upload support in env/ui diff -r 594a85118cb7 -r 36ebcae1cde3 framework/env/ui/field/file.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/ui/field/file.lua Sun Dec 18 22:09:58 2011 +0100 @@ -0,0 +1,24 @@ +--[[-- +ui.field.file{ + ... -- generic ui.field.* arguments, as described for ui.autofield{...} +} + +This function inserts a field for uploading a file in the active slot. For read-only forms this function does nothing. For description of the generic field helper arguments, see help for ui.autofield{...}. + +--]]-- + +function ui.field.file(args) + ui.form_element(args, nil, function(args) + if args.readonly then + -- nothing + else + if not slot.get_state_table().form_file_upload then + error('Parameter "file_upload" of ui.form{...} must be set to true to allow file uploads.') + end + local attr = table.new(args.attr) + attr.type = "file" + attr.name = args.html_name + ui.tag{ tag = "input", attr = attr } + end + end) +end diff -r 594a85118cb7 -r 36ebcae1cde3 framework/env/ui/form.lua --- a/framework/env/ui/form.lua Sun Dec 18 21:14:43 2011 +0100 +++ b/framework/env/ui/form.lua Sun Dec 18 22:09:58 2011 +0100 @@ -1,11 +1,12 @@ --[[-- ui.form{ - record = record, -- optional record to be used - read_only = read_only, -- set to true, if form should be read-only (no submit button) - external = external, -- external URL to be used as HTML form action - module = module, -- module name to be used for HTML form action - view = view, -- view name to be used for HTML form action - action = action, -- action name to be used for HTML form action + record = record, -- optional record to be used + read_only = read_only, -- set to true, if form should be read-only (no submit button) + file_upload = file_upload, -- must be set to true, if form contains file upload element + external = external, -- external URL to be used as HTML form action + module = module, -- module name to be used for HTML form action + view = view, -- view name to be used for HTML form action + action = action, -- action name to be used for HTML form action routing = { default = { -- default routing for called action mode = mode, -- "forward" or "redirect" @@ -18,7 +19,7 @@ error = { ... }, -- routing when "error" is returned by the called action ... = { ... } -- routing when "..." is returned by the called action }, - partial = { -- parameters for partial loading, see below + partial = { -- parameters for partial loading, see below module = module, view = view, id = id, @@ -70,8 +71,9 @@ function ui.form(args) local args = args or {} local slot_state = slot.get_state_table() - local old_record = slot_state.form_record - local old_readonly = slot_state.form_readonly + local old_record = slot_state.form_record + local old_readonly = slot_state.form_readonly + local old_file_upload = slot_state.form_file_upload slot_state.form_record = args.record if args.readonly then slot_state.form_readonly = true @@ -82,6 +84,13 @@ prepare_routing_params(params, args.routing, args.module) params._webmcp_csrf_secret = request.get_csrf_secret() local attr = table.new(args.attr) + if attr.enctype=="multipart/form-data" or slot_state.form_file_upload then + slot_state.form_file_upload = true + if attr.enctype == nil then + attr.enctype = "multipart/form-data" + end + end + end attr.action = encode.url{ external = args.external, module = args.module or request.get_module(), @@ -149,6 +158,7 @@ } slot_state.form_opened = false end - slot_state.form_readonly = old_readonly - slot_state.form_record = old_record + slot_state.form_file_upload = old_file_upload + slot_state.form_readonly = old_readonly + slot_state.form_record = old_record end