liquid_feedback_frontend
view app/main/registration/_register_form.lua @ 1684:b1cec2dcc035
Added support for other option in dropdown fields
| author | bsw |
|---|---|
| date | Mon Sep 20 12:32:40 2021 +0200 (2021-09-20) |
| parents | af7b5bc0dec2 |
| children | d09c3a0d17e6 |
line source
1 for i, field in ipairs(config.self_registration.fields) do
2 local class = ""
3 local field_error = slot.get_content("self_registration__invalid_" .. field.name)
4 if field_error == "" then
5 field_error = nil
6 end
7 if field_error then
8 class = " is-invalid"
9 end
10 if field.title then
11 ui.container{ attr = { style = "font-weight: bold;" }, content = field.title }
12 end
13 if field.text then
14 ui.container{ content = field.text }
15 end
16 if not field.internal then
17 if field.type == "comment" then
18 ui.tag { content = field.label }
19 elseif field.name == "date_of_birth" then
20 local label = field.label
21 if field.optional then
22 label = label .. config.self_registration.optional_field_indicator
23 end
24 ui.tag{ tag = "label", attr = { style = "vertical-align: bottom; border-bottom: 1px solid rgba(0,0,0, 0.12); color: #777; font-size: 16px;" }, content = field.label .. ":" }
25 slot.put(" ")
26 local days = { { id = 0, name = _"day" } }
27 for i = 1, 31 do
28 table.insert(days, { id = i, name = i })
29 end
30 local months = {
31 { id = 0, name = _"month" },
32 { id = 1, name = "gennaio" },
33 { id = 2, name = "febbraio" },
34 { id = 3, name = "marzo" },
35 { id = 4, name = "aprile" },
36 { id = 5, name = "maggio" },
37 { id = 6, name = "giugno" },
38 { id = 7, name = "luglio" },
39 { id = 8, name = "agosto" },
40 { id = 9, name = "settembre" },
41 { id = 10, name = "ottobre" },
42 { id = 11, name = "novembre" },
43 { id = 12, name = "dicembre" },
44 }
45 if config.self_registration.lang == "en" then
46 months = {
47 { id = 0, name = _"month" },
48 { id = 1, name = "January" },
49 { id = 2, name = "February" },
50 { id = 3, name = "March" },
51 { id = 4, name = "April" },
52 { id = 5, name = "May" },
53 { id = 6, name = "June" },
54 { id = 7, name = "July" },
55 { id = 8, name = "August" },
56 { id = 9, name = "September" },
57 { id = 10, name = "October" },
58 { id = 11, name = "November" },
59 { id = 12, name = "December" },
60 }
61 end
62 if config.self_registration.lang == "de" then
63 months = {
64 { id = 0, name = _"month" },
65 { id = 1, name = "Januar" },
66 { id = 2, name = "Februar" },
67 { id = 3, name = "März" },
68 { id = 4, name = "April" },
69 { id = 5, name = "Mai" },
70 { id = 6, name = "Juni" },
71 { id = 7, name = "Juli" },
72 { id = 8, name = "August" },
73 { id = 9, name = "September" },
74 { id = 10, name = "Oktober" },
75 { id = 11, name = "November" },
76 { id = 12, name = "Dezember" },
77 }
78 end
79 local years = { { id = 0, name = _"year" } }
80 local min_age = config.self_registration.min_age or 16
81 for i = (atom.date:get_current()).year - min_age, 1900, -1 do
82 table.insert(years, { id = i, name = i })
83 end
84 ui.field.select{
85 container_attr = { style = "display: inline-block; " },
86 attr = { class = class },
87 foreign_records = days,
88 foreign_id = "id",
89 foreign_name = "name",
90 name = "verification_data_" .. field.name .. "_day",
91 value = tonumber(request.get_param{ name = "verification_data_" .. field.name .. "_day" })
92 }
93 slot.put(" ")
94 ui.field.select{
95 container_attr = { style = "display: inline-block; " },
96 attr = { class = class },
97 foreign_records = months,
98 foreign_id = "id",
99 foreign_name = "name",
100 name = "verification_data_" .. field.name .. "_month",
101 value = tonumber(request.get_param{ name = "verification_data_" .. field.name .. "_month" })
102 }
103 slot.put(" ")
104 ui.field.select{
105 container_attr = { style = "display: inline-block; " },
106 attr = { class = class },
107 foreign_records = years,
108 foreign_id = "id",
109 foreign_name = "name",
110 name = "verification_data_" .. field.name .. "_year",
111 value = tonumber(request.get_param{ name = "verification_data_" .. field.name .. "_year" })
112 }
113 slot.put("<br />")
115 elseif field.type == "dropdown" then
116 local options = { { id = "", name = "" } }
117 local other_option_id
118 for i_options, option in ipairs(field.options) do
119 if not option.id then
120 option.id = option.name
121 end
122 if option.other then
123 other_option_id = option.id
124 end
125 table.insert(options, option)
126 end
127 if field.label then
128 ui.tag{ tag = "label", attr = { style = "vertical-align: bottom; border-bottom: 1px solid rgba(0,0,0, 0.12); color: #777; font-size: 16px;" }, content = field.label .. ":" }
129 slot.put(" ")
130 end
131 local onchange_script
132 if other_option_id then
133 onchange_script = "var el = document.getElementById('lf-register__data_other_container_" .. field.name .. "'); if (this.value == '" .. other_option_id .. "') { console.log(el); el.classList.remove('hidden'); } else { el.classList.add('hidden'); };"
134 end
135 ui.field.select{
136 container_attr = { style = "display: inline-block; " },
137 attr = { class = class, onchange = onchange_script },
138 foreign_records = options,
139 foreign_id = "id",
140 foreign_name = "name",
141 name = "verification_data_" .. field.name,
142 value = tonumber(request.get_param{ name = "verification_data_" .. field.name })
143 }
144 if other_option_id then
145 slot.put(" ")
146 ui.field.text{
147 container_attr = { id = "lf-register__data_other_container_" .. field.name, class = "hidden mdl-textfield mdl-js-textfield mdl-textfield--floating-label" .. class },
148 attr = { id = "lf-register__data_other_" .. field.name, class = "mdl-textfield__input" },
149 name = "verification_data_" .. field.name .. "_other",
150 label_attr = { class = "mdl-textfield__label", ["for"] = "lf-register__data" .. field.name },
151 label = field.name,
152 value = request.get_param{ name = "verification_data_" .. field.name .. "_other" }
153 }
154 end
155 slot.put("<br />")
157 elseif field.type == "image" then
158 slot.put("<br />")
159 ui.tag{ tag = "label", attr = { style = "vertical-align: bottom; border-bottom: 1px solid rgba(0,0,0, 0.12); color: #777; font-size: 16px;" }, content = field.label .. ":" }
160 slot.put("<br />")
161 ui.script{ script = [[
162 function getFile(){
163 document.getElementById("fileInput").click();
164 }
165 function fileChoosen(obj){
166 var file = obj.value;
167 var fileName = file.split("\\");
168 var checked = false;
169 var label = "]] .. field.upload_label .. [[";
170 if (fileName[fileName.length-1].length > 0) {
171 label = fileName[fileName.length-1];
172 }
173 document.getElementById("fileBtn").innerHTML = label;
174 event.preventDefault();
175 }
176 ]] }
177 ui.tag{ tag = "input", attr = { id = "fileInput", style = "display: none;", type = "file", name = "verification_data_" .. field.name, onchange = "fileChoosen(this);" } }
178 ui.tag{
179 attr = { id = "fileBtn", class = "mdl-button mdl-js-button mdl-button--underlined", onclick = "getFile();" },
180 content = field.upload_label
181 }
182 if field.optional_checkbox then
183 slot.put(" ")
184 ui.tag{ tag = "label", attr = {
185 class = "mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect",
186 style = "display: inline;",
187 ["for"] = "verification_data_" .. field.name .. "_optout"
188 },
189 content = function()
190 ui.tag{
191 tag = "input",
192 attr = {
193 type = "checkbox",
194 class = "mdl-checkbox__input",
195 id = "verification_data_" .. field.name .. "_optout",
196 name = "verification_data_" .. field.name .. "_optout",
197 value = "1",
198 style = "float: left;",
199 checked = request.get_param{ name = "verification_data_" .. field.name .. "_optout" } and "checked" or nil,
200 }
201 }
202 ui.tag{
203 attr = { class = "mdl-checkbox__label" },
204 content = field.optional_checkbox
205 }
206 end
207 }
208 end
210 elseif field.name == "unit" then
211 local units_selector = Unit:new_selector()
212 :add_where{ "active" }
213 :add_order_by("name")
214 if field.where then
215 units_selector:add_where(field.where)
216 end
217 local units = { { id = "", name = "" } }
218 if field.optional then
219 table.insert(units, {
220 id = "",
221 name = _"none"
222 })
223 end
224 for i_unit, unit in ipairs(units_selector:exec()) do
225 table.insert(units, unit)
226 end
227 ui.tag{ tag = "label", attr = { style = "vertical-align: bottom; border-bottom: 1px solid rgba(0,0,0, 0.12); color: #777; font-size: 16px;" }, content = field.label .. ":" }
228 slot.put(" ")
229 ui.field.select{
230 container_attr = { style = "display: inline-block; " },
231 foreign_records = units,
232 foreign_id = "id",
233 foreign_name = "name",
234 name = "verification_data_" .. field.name,
235 value = tonumber(request.get_param{ name = "verification_data_" .. field.name })
236 }
237 slot.put("<br />")
238 else
239 if field.name == "mobile_phone" then
240 if config.self_registration.lang ~= "en" then
241 ui.tag{ content = "+39" }
242 slot.put(" ")
243 end
244 end
245 local label = field.label
246 if field.optional then
247 label = label .. config.self_registration.optional_field_indicator
248 end
249 ui.field.text{
250 container_attr = { class = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label" .. class },
251 attr = { id = "lf-register__data_" .. field.name, class = "mdl-textfield__input" },
252 label_attr = { class = "mdl-textfield__label", ["for"] = "lf-register__data" .. field.name },
253 label = label,
254 name = "verification_data_" .. field.name,
255 value = request.get_param{ name = "verification_data_" .. field.name }
256 }
257 end
258 slot.put("<br />")
259 end
260 end
