Comparison

util/dataforms.lua @ 9121:e5eb36ee07a2

util.dataforms: Allow passing the current values to be used in stead of omitted fields
author Kim Alvefur <zash@zash.se>
date Sun, 05 Aug 2018 15:50:06 +0200
parent 9111:bc7473fb7ad3
child 9242:68694c1bd960
comparison
equal deleted inserted replaced
9120:01fc8ba0a7fa 9121:e5eb36ee07a2
140 return form; 140 return form;
141 end 141 end
142 142
143 local field_readers = {}; 143 local field_readers = {};
144 144
145 function form_t.data(layout, stanza) 145 function form_t.data(layout, stanza, current)
146 local data = {}; 146 local data = {};
147 local errors = {}; 147 local errors = {};
148 local present = {}; 148 local present = {};
149 149
150 for _, field in ipairs(layout) do 150 for _, field in ipairs(layout) do
155 break; 155 break;
156 end 156 end
157 end 157 end
158 158
159 if not tag then 159 if not tag then
160 if field.required then 160 if current and current[field.name] ~= nil then
161 data[field.name] = current[field.name];
162 elseif field.required then
161 errors[field.name] = "Required value missing"; 163 errors[field.name] = "Required value missing";
162 end 164 end
163 elseif field.name then 165 elseif field.name then
164 present[field.name] = true; 166 present[field.name] = true;
165 local reader = field_readers[field.type]; 167 local reader = field_readers[field.type];