webmcp
view framework/env/ui_deprecated/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 | 
 line source
     1 --
     2 -- Creates a formular
     3 --
     4 -- record          (record)   Take field values from this object (optional)
     5 -- class           (string)   Style class (optional)
     6 -- method          (string)   Submit method ['post', 'get'] (optional)
     7 -- module          (string)   The module to submit to
     8 -- action          (string)   The action to submit to
     9 -- params          (table)    The GET parameter to be send with
    10 -- content         (function) Function for the content of the form
    11 --
    12 -- Example:
    13 --
    14 --  ui_deprecated.form({
    15 --    object = client,
    16 --    class  = 'form_class',
    17 --    method = 'post',
    18 --    module = 'client',
    19 --    action = 'update',
    20 --    params = {
    21 --      id = client.id
    22 --    }, 
    23 --    redirect_to = {
    24 --      ok = {
    25 --        module = 'client', 
    26 --        view = 'list'
    27 --        },
    28 --      error = {
    29 --        module = 'client', 
    30 --        view = 'edit', 
    31 --        params = { 
    32 --          id = client.id
    33 --        }
    34 --      }
    35 --    },
    36 --  })
    38 function ui_deprecated.form(args)
    39   local slot_state = slot.get_state_table()
    40   if slot_state.form_opened then
    41     error("Cannot open a form inside a form.")
    42   end
    43   slot_state.form_opened = true
    44   local old_record = slot_state.form_record
    45   slot_state.form_record = args.record or {}  -- TODO: decide what really should happen when no record is specified
    47   local params = {}
    48   if args.params then
    49     for key, value in pairs(args.params) do
    50       params[key] = value
    51     end
    52   end
    53   ui_deprecated._prepare_redirect_params(params, args.redirect_to)
    55   local attr_action = args.url or encode.url{
    56     module = args.module,
    57     view   = args.view,
    58     action = args.action,
    59     id     = args.id,
    60     params = params
    61   }
    63   local base = request.get_relative_baseurl()
    64   local attr_class = table.concat({ 'ui_form', args.class }, ' ')
    65   local attr_method = args.method or 'POST'  -- TODO: uppercase/sanatize method
    67   slot.put(
    68     '<form',
    69     ' action="', attr_action, '"',
    70     ' class="',  attr_class,  '"',
    71     ' method="', attr_method, '"',
    72     '>\n'
    73   )
    75   if type(args.content) == 'function' then
    76     args.content()
    77   else
    78     error('No content function')
    79   end
    81   slot.put('</form>')
    82   slot_state.form_record = old_record
    83   slot_state.form_opened = false
    85 end
