webmcp
diff framework/env/ui_deprecated/form.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/ui_deprecated/form.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +-- 1.5 +-- Creates a formular 1.6 +-- 1.7 +-- record (record) Take field values from this object (optional) 1.8 +-- class (string) Style class (optional) 1.9 +-- method (string) Submit method ['post', 'get'] (optional) 1.10 +-- module (string) The module to submit to 1.11 +-- action (string) The action to submit to 1.12 +-- params (table) The GET parameter to be send with 1.13 +-- content (function) Function for the content of the form 1.14 +-- 1.15 +-- Example: 1.16 +-- 1.17 +-- ui_deprecated.form({ 1.18 +-- object = client, 1.19 +-- class = 'form_class', 1.20 +-- method = 'post', 1.21 +-- module = 'client', 1.22 +-- action = 'update', 1.23 +-- params = { 1.24 +-- id = client.id 1.25 +-- }, 1.26 +-- redirect_to = { 1.27 +-- ok = { 1.28 +-- module = 'client', 1.29 +-- view = 'list' 1.30 +-- }, 1.31 +-- error = { 1.32 +-- module = 'client', 1.33 +-- view = 'edit', 1.34 +-- params = { 1.35 +-- id = client.id 1.36 +-- } 1.37 +-- } 1.38 +-- }, 1.39 +-- }) 1.40 + 1.41 +function ui_deprecated.form(args) 1.42 + local slot_state = slot.get_state_table() 1.43 + if slot_state.form_opened then 1.44 + error("Cannot open a form inside a form.") 1.45 + end 1.46 + slot_state.form_opened = true 1.47 + local old_record = slot_state.form_record 1.48 + slot_state.form_record = args.record or {} -- TODO: decide what really should happen when no record is specified 1.49 + 1.50 + local params = {} 1.51 + if args.params then 1.52 + for key, value in pairs(args.params) do 1.53 + params[key] = value 1.54 + end 1.55 + end 1.56 + ui_deprecated._prepare_redirect_params(params, args.redirect_to) 1.57 + 1.58 + local attr_action = args.url or encode.url{ 1.59 + module = args.module, 1.60 + view = args.view, 1.61 + action = args.action, 1.62 + id = args.id, 1.63 + params = params 1.64 + } 1.65 + 1.66 + local base = request.get_relative_baseurl() 1.67 + local attr_class = table.concat({ 'ui_form', args.class }, ' ') 1.68 + local attr_method = args.method or 'POST' -- TODO: uppercase/sanatize method 1.69 + 1.70 + slot.put( 1.71 + '<form', 1.72 + ' action="', attr_action, '"', 1.73 + ' class="', attr_class, '"', 1.74 + ' method="', attr_method, '"', 1.75 + '>\n' 1.76 + ) 1.77 + 1.78 + if type(args.content) == 'function' then 1.79 + args.content() 1.80 + else 1.81 + error('No content function') 1.82 + end 1.83 + 1.84 + slot.put('</form>') 1.85 + slot_state.form_record = old_record 1.86 + slot_state.form_opened = false 1.87 + 1.88 +end