Comparison

util/dataforms.lua @ 6981:bcaa553de6e8

util.dataforms: Track which fields are included in a form
author Kim Alvefur <zash@zash.se>
date Tue, 08 Dec 2015 20:16:49 +0100
parent 6830:e08f5d081a6c
child 7442:156c95bfed0b
comparison
equal deleted inserted replaced
6980:daaa52b3f45f 6981:bcaa553de6e8
116 local field_readers = {}; 116 local field_readers = {};
117 117
118 function form_t.data(layout, stanza) 118 function form_t.data(layout, stanza)
119 local data = {}; 119 local data = {};
120 local errors = {}; 120 local errors = {};
121 local present = {};
121 122
122 for _, field in ipairs(layout) do 123 for _, field in ipairs(layout) do
123 local tag; 124 local tag;
124 for field_tag in stanza:childtags("field") do 125 for field_tag in stanza:childtags("field") do
125 if field.name == field_tag.attr.var then 126 if field.name == field_tag.attr.var then
131 if not tag then 132 if not tag then
132 if field.required then 133 if field.required then
133 errors[field.name] = "Required value missing"; 134 errors[field.name] = "Required value missing";
134 end 135 end
135 else 136 else
137 present[field.name] = true;
136 local reader = field_readers[field.type]; 138 local reader = field_readers[field.type];
137 if reader then 139 if reader then
138 data[field.name], errors[field.name] = reader(tag, field.required); 140 data[field.name], errors[field.name] = reader(tag, field.required);
139 end 141 end
140 end 142 end
141 end 143 end
142 if next(errors) then 144 if next(errors) then
143 return data, errors; 145 return data, errors, present;
144 end 146 end
145 return data; 147 return data, nil, present;
146 end 148 end
147 149
148 local function simple_text(field_tag, required) 150 local function simple_text(field_tag, required)
149 local data = field_tag:get_child_text("value"); 151 local data = field_tag:get_child_text("value");
150 -- XEP-0004 does not say if an empty string is acceptable for a required value 152 -- XEP-0004 does not say if an empty string is acceptable for a required value