← All changes
|
includes/widgets/Form_Builder/editor-handler.js
+40
-29
51.1.64
→
51.1.83
View file →
| @@ -183,45 +183,36 @@ | ||
| 183 | 183 | zipField, |
| 184 | 184 | ]; |
| 185 | 185 | |
| 186 | 186 | const options = {none: "None"}; |
| 187 | - let prevFieldId; | |
| 187 | + const seenIds = {}; | |
| 188 | + $panelRoot.find(".king-addons-field-id-dup-notice").remove(); | |
| 188 | 189 | |
| 189 | 190 | formFieldsModel.each(function (field) { |
| 190 | 191 | const fieldLabel = field.get("field_label"); |
| 191 | - const fieldId = field.get("field_id"); | |
| 192 | + let fieldId = (field.get("field_id") || "").toString().trim(); | |
| 193 | + const fallbackId = (field.get("_id") || "").toString(); | |
| 192 | 194 | |
| 193 | - if (prevFieldId === fieldId) { | |
| 194 | - $panelRoot | |
| 195 | - .find(":input[value=" + field.attributes._id + "]") | |
| 196 | - .closest(".elementor-repeater-fields") | |
| 197 | - .find(':input[data-setting="field_id"]') | |
| 198 | - .val(field.attributes._id); | |
| 199 | - $panelRoot | |
| 200 | - .find(":input[value=" + field.attributes._id + "]") | |
| 201 | - .closest(".elementor-repeater-fields") | |
| 202 | - .find(".king-addons-form-field-shortcode") | |
| 203 | - .val('id=["' + field.attributes._id + '"]'); | |
| 204 | - field.attributes.field_id = field.attributes._id; | |
| 195 | + if (!fieldId && fallbackId) { | |
| 196 | + assignFieldId(field, fallbackId); | |
| 197 | + fieldId = fallbackId; | |
| 205 | 198 | } |
| 206 | 199 | |
| 207 | - prevFieldId = fieldId; | |
| 200 | + if (fieldId && seenIds[fieldId]) { | |
| 201 | + const $wrap = $panelRoot.find(".elementor-repeater-fields-wrapper").first(); | |
| 202 | + if ($wrap.length && !$panelRoot.find(".king-addons-field-id-dup-notice").length) { | |
| 203 | + $wrap.prepend( | |
| 204 | + '<div class="elementor-panel-alert elementor-panel-alert-warning king-addons-field-id-dup-notice">' + | |
| 205 | + "Two fields share the same Field ID. Each ID must be unique or submitted values will collide." + | |
| 206 | + "</div>" | |
| 207 | + ); | |
| 208 | + } | |
| 209 | + } | |
| 208 | 210 | |
| 209 | - if (!fieldId) { | |
| 210 | - $panelRoot | |
| 211 | - .find(":input[value=" + field.attributes._id + "]") | |
| 212 | - .closest(".elementor-repeater-fields") | |
| 213 | - .find(':input[data-setting="field_id"]') | |
| 214 | - .val(field.attributes._id); | |
| 215 | - $panelRoot | |
| 216 | - .find(":input[value=" + field.attributes._id + "]") | |
| 217 | - .closest(".elementor-repeater-fields") | |
| 218 | - .find(".king-addons-form-field-shortcode") | |
| 219 | - .val('id=["' + field.attributes._id + '"]'); | |
| 220 | - field.attributes.field_id = field.attributes._id; | |
| 211 | + if (fieldId) { | |
| 212 | + seenIds[fieldId] = true; | |
| 213 | + options[fieldId] = fieldLabel; | |
| 221 | 214 | } |
| 222 | - | |
| 223 | - options[fieldId] = fieldLabel; | |
| 224 | 215 | }); |
| 225 | 216 | |
| 226 | 217 | view.model.setSetting("email_field", _.extend(emailField, {options})); |
| 227 | 218 | view.model.setSetting("first_name_field", _.extend(firstNameField, {options})); |
| @@ -245,8 +236,28 @@ | ||
| 245 | 236 | '<option value="' + value + '"' + isSelected + ">" + label + "</option>" |
| 246 | 237 | ); |
| 247 | 238 | }); |
| 248 | 239 | }); |
| 240 | + } | |
| 241 | + | |
| 242 | + function assignFieldId(field, nextId) { | |
| 243 | + if (!field || !nextId) { | |
| 244 | + return; | |
| 245 | + } | |
| 246 | + | |
| 247 | + if (typeof field.setSetting === "function") { | |
| 248 | + field.setSetting("field_id", nextId); | |
| 249 | + } else if (typeof field.set === "function") { | |
| 250 | + field.set("field_id", nextId); | |
| 251 | + } else { | |
| 252 | + field.attributes.field_id = nextId; | |
| 253 | + } | |
| 254 | + | |
| 255 | + const $row = $panelRoot | |
| 256 | + .find(":input[value=" + field.attributes._id + "]") | |
| 257 | + .closest(".elementor-repeater-fields"); | |
| 258 | + $row.find(':input[data-setting="field_id"]').val(nextId); | |
| 259 | + $row.find(".king-addons-form-field-shortcode").val('id=["' + nextId + '"]'); | |
| 249 | 260 | } |
| 250 | 261 | |
| 251 | 262 | // Change the selector below to match the field_id field in your repeater. |
| 252 | 263 | const customIdFieldSelector = 'input[data-setting="field_id"]'; |