webmcp

changeset 56:36ebcae1cde3

File upload support in env/ui
author jbe
date Sun Dec 18 22:09:58 2011 +0100 (2011-12-18)
parents 594a85118cb7
children 6a830c1479b0
files framework/env/ui/field/file.lua framework/env/ui/form.lua
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/field/file.lua	Sun Dec 18 22:09:58 2011 +0100
     1.3 @@ -0,0 +1,24 @@
     1.4 +--[[--
     1.5 +ui.field.file{
     1.6 +  ...                        -- generic ui.field.* arguments, as described for ui.autofield{...}
     1.7 +}
     1.8 +
     1.9 +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{...}.
    1.10 +
    1.11 +--]]--
    1.12 +
    1.13 +function ui.field.file(args)
    1.14 +  ui.form_element(args, nil, function(args)
    1.15 +    if args.readonly then
    1.16 +      -- nothing
    1.17 +    else
    1.18 +      if not slot.get_state_table().form_file_upload then
    1.19 +        error('Parameter "file_upload" of ui.form{...} must be set to true to allow file uploads.')
    1.20 +      end
    1.21 +      local attr = table.new(args.attr)
    1.22 +      attr.type  = "file"
    1.23 +      attr.name  = args.html_name
    1.24 +      ui.tag{ tag  = "input", attr = attr }
    1.25 +    end
    1.26 +  end)
    1.27 +end
     2.1 --- a/framework/env/ui/form.lua	Sun Dec 18 21:14:43 2011 +0100
     2.2 +++ b/framework/env/ui/form.lua	Sun Dec 18 22:09:58 2011 +0100
     2.3 @@ -1,11 +1,12 @@
     2.4  --[[--
     2.5  ui.form{
     2.6 -  record    = record,     -- optional record to be used 
     2.7 -  read_only = read_only,  -- set to true, if form should be read-only (no submit button)
     2.8 -  external  = external,   -- external URL to be used as HTML form action
     2.9 -  module    = module,     -- module name to be used for HTML form action
    2.10 -  view      = view,       -- view name   to be used for HTML form action
    2.11 -  action    = action,     -- action name to be used for HTML form action
    2.12 +  record      = record,       -- optional record to be used 
    2.13 +  read_only   = read_only,    -- set to true, if form should be read-only (no submit button)
    2.14 +  file_upload = file_upload,  -- must be set to true, if form contains file upload element
    2.15 +  external    = external,     -- external URL to be used as HTML form action
    2.16 +  module      = module,       -- module name to be used for HTML form action
    2.17 +  view        = view,         -- view name   to be used for HTML form action
    2.18 +  action      = action,       -- action name to be used for HTML form action
    2.19    routing = {
    2.20      default = {           -- default routing for called action
    2.21        mode   = mode,      -- "forward" or "redirect"
    2.22 @@ -18,7 +19,7 @@
    2.23      error = { ... },      -- routing when "error" is returned by the called action
    2.24      ...   = { ... }       -- routing when "..."   is returned by the called action
    2.25    },
    2.26 -  partial   = {           -- parameters for partial loading, see below
    2.27 +  partial = {             -- parameters for partial loading, see below
    2.28      module = module,
    2.29      view   = view,
    2.30      id     = id,
    2.31 @@ -70,8 +71,9 @@
    2.32  function ui.form(args)
    2.33    local args = args or {}
    2.34    local slot_state = slot.get_state_table()
    2.35 -  local old_record   = slot_state.form_record
    2.36 -  local old_readonly = slot_state.form_readonly
    2.37 +  local old_record      = slot_state.form_record
    2.38 +  local old_readonly    = slot_state.form_readonly
    2.39 +  local old_file_upload = slot_state.form_file_upload
    2.40    slot_state.form_record = args.record
    2.41    if args.readonly then
    2.42      slot_state.form_readonly = true
    2.43 @@ -82,6 +84,13 @@
    2.44      prepare_routing_params(params, args.routing, args.module)
    2.45      params._webmcp_csrf_secret = request.get_csrf_secret()
    2.46      local attr = table.new(args.attr)
    2.47 +    if attr.enctype=="multipart/form-data" or slot_state.form_file_upload then
    2.48 +      slot_state.form_file_upload = true
    2.49 +      if attr.enctype == nil then
    2.50 +        attr.enctype = "multipart/form-data"
    2.51 +      end
    2.52 +    end
    2.53 +    end
    2.54      attr.action = encode.url{
    2.55        external  = args.external,
    2.56        module    = args.module or request.get_module(),
    2.57 @@ -149,6 +158,7 @@
    2.58      }
    2.59      slot_state.form_opened = false
    2.60    end
    2.61 -  slot_state.form_readonly = old_readonly
    2.62 -  slot_state.form_record   = old_record
    2.63 +  slot_state.form_file_upload = old_file_upload
    2.64 +  slot_state.form_readonly    = old_readonly
    2.65 +  slot_state.form_record      = old_record
    2.66  end

Impressum / About Us