|
95 | 95 | return "This field is required"; |
96 | 96 | } |
97 | 97 |
|
98 | | - if (value && value !== "") { |
| 98 | + if (value !== null && value !== undefined && value !== "") { |
99 | 99 | switch (fieldDef.type) { |
100 | 100 | case "integer": |
101 | | - if (!/^-?\d+$/.test(value.toString())) { |
102 | | - return "Must be a valid integer"; |
103 | | - } |
104 | | - break; |
105 | 101 | case "number": |
106 | | - if (isNaN(Number(value))) { |
| 102 | + const num = Number(value); |
| 103 | + if (isNaN(num)) { |
107 | 104 | return "Must be a valid number"; |
108 | 105 | } |
| 106 | + if (fieldDef.type === "integer" && !Number.isInteger(num)) { |
| 107 | + return "Must be an integer"; |
| 108 | + } |
| 109 | + if (fieldDef.minimum !== undefined && num < fieldDef.minimum) { |
| 110 | + return `Must be at least ${fieldDef.minimum}`; |
| 111 | + } |
| 112 | + if (fieldDef.maximum !== undefined && num > fieldDef.maximum) { |
| 113 | + return `Must be at most ${fieldDef.maximum}`; |
| 114 | + } |
109 | 115 | break; |
110 | 116 | case "boolean": |
111 | 117 | // Accept true/false or convert from string |
|
115 | 121 | return "Must be in format YYYY-MM-DD"; |
116 | 122 | } |
117 | 123 | break; |
118 | | - } |
119 | | -
|
120 | | - if (fieldDef.minLength && value.length < fieldDef.minLength) { |
121 | | - return `Must be at least ${fieldDef.minLength} characters`; |
122 | | - } |
123 | | - if (fieldDef.maxLength && value.length > fieldDef.maxLength) { |
124 | | - return `Must be at most ${fieldDef.maxLength} characters`; |
| 124 | + default: |
| 125 | + // String validation |
| 126 | + if (fieldDef.minLength && String(value).length < fieldDef.minLength) { |
| 127 | + return `Must be at least ${fieldDef.minLength} characters`; |
| 128 | + } |
| 129 | + if (fieldDef.maxLength && String(value).length > fieldDef.maxLength) { |
| 130 | + return `Must be at most ${fieldDef.maxLength} characters`; |
| 131 | + } |
| 132 | + break; |
125 | 133 | } |
126 | 134 | } |
127 | 135 |
|
|
143 | 151 | return isValid; |
144 | 152 | } |
145 | 153 |
|
| 154 | + function convertFormDataToApiFormat( |
| 155 | + data: Record<string, any>, |
| 156 | + ): Record<string, any> { |
| 157 | + const converted: Record<string, any> = {}; |
| 158 | +
|
| 159 | + Object.keys(properties).forEach((fieldName) => { |
| 160 | + const fieldDef = properties[fieldName]; |
| 161 | + const value = data[fieldName]; |
| 162 | +
|
| 163 | + // Skip empty values |
| 164 | + if (value === "" || value === null || value === undefined) { |
| 165 | + return; |
| 166 | + } |
| 167 | +
|
| 168 | + switch (fieldDef.type) { |
| 169 | + case "boolean": |
| 170 | + // Convert boolean to string "true" or "false" |
| 171 | + converted[fieldName] = value ? "true" : "false"; |
| 172 | + break; |
| 173 | + case "integer": |
| 174 | + // Convert to integer number |
| 175 | + const intValue = Number(value); |
| 176 | + converted[fieldName] = Math.round(intValue); |
| 177 | + break; |
| 178 | + case "number": |
| 179 | + // Convert to number (JavaScript/JSON doesn't distinguish int vs float) |
| 180 | + converted[fieldName] = Number(value); |
| 181 | + break; |
| 182 | + default: |
| 183 | + // Keep as string |
| 184 | + converted[fieldName] = String(value); |
| 185 | + break; |
| 186 | + } |
| 187 | + }); |
| 188 | +
|
| 189 | + return converted; |
| 190 | + } |
| 191 | +
|
146 | 192 | async function handleCreate() { |
147 | 193 | if (!validateAllFields()) { |
148 | 194 | alert("Please fix validation errors"); |
|
152 | 198 | isSubmitting = true; |
153 | 199 |
|
154 | 200 | try { |
| 201 | + const convertedData = convertFormDataToApiFormat(formData); |
| 202 | + console.log("Original formData:", formData); |
| 203 | + console.log("Converted data being sent:", convertedData); |
| 204 | + console.log( |
| 205 | + "Converted data JSON:", |
| 206 | + JSON.stringify(convertedData, null, 2), |
| 207 | + ); |
| 208 | +
|
155 | 209 | const response = await fetch( |
156 | 210 | `/api/dynamic-entities/${entity.dynamicEntityId}/data`, |
157 | 211 | { |
158 | 212 | method: "POST", |
159 | 213 | headers: { |
160 | 214 | "Content-Type": "application/json", |
161 | 215 | }, |
162 | | - body: JSON.stringify(formData), |
| 216 | + credentials: "include", |
| 217 | + body: JSON.stringify(convertedData), |
163 | 218 | }, |
164 | 219 | ); |
165 | 220 |
|
166 | 221 | if (!response.ok) { |
167 | | - const error = await response.json(); |
168 | | - throw new Error(error.message || "Failed to create record"); |
| 222 | + let errorMessage = "Failed to create record"; |
| 223 | + try { |
| 224 | + const error = await response.json(); |
| 225 | + errorMessage = error.error || error.message || errorMessage; |
| 226 | + console.error("API Error Response:", error); |
| 227 | + } catch (e) { |
| 228 | + const text = await response.text(); |
| 229 | + console.error("Non-JSON Error Response:", text); |
| 230 | + errorMessage = `Server error: ${response.status} ${response.statusText}`; |
| 231 | + } |
| 232 | + throw new Error(errorMessage); |
169 | 233 | } |
170 | 234 |
|
171 | 235 | const result = await response.json(); |
172 | 236 | dataRecords = [...dataRecords, result]; |
173 | 237 | alert("Record created successfully"); |
174 | 238 | closeModals(); |
175 | 239 | } catch (error) { |
176 | | - alert(error instanceof Error ? error.message : "Failed to create record"); |
| 240 | + const errorMsg = |
| 241 | + error instanceof Error ? error.message : "Failed to create record"; |
| 242 | + alert(`Error: ${errorMsg}`); |
177 | 243 | console.error("Create error:", error); |
178 | 244 | } finally { |
179 | 245 | isSubmitting = false; |
|
194 | 260 | isSubmitting = true; |
195 | 261 |
|
196 | 262 | try { |
| 263 | + const convertedData = convertFormDataToApiFormat(formData); |
| 264 | +
|
197 | 265 | const response = await fetch( |
198 | 266 | `/api/dynamic-entities/${entity.dynamicEntityId}/data/${selectedRecord.id}`, |
199 | 267 | { |
200 | 268 | method: "PUT", |
201 | 269 | headers: { |
202 | 270 | "Content-Type": "application/json", |
203 | 271 | }, |
204 | | - body: JSON.stringify(formData), |
| 272 | + credentials: "include", |
| 273 | + body: JSON.stringify(convertedData), |
205 | 274 | }, |
206 | 275 | ); |
207 | 276 |
|
208 | 277 | if (!response.ok) { |
209 | | - const error = await response.json(); |
210 | | - throw new Error(error.message || "Failed to update record"); |
| 278 | + let errorMessage = "Failed to update record"; |
| 279 | + try { |
| 280 | + const error = await response.json(); |
| 281 | + errorMessage = error.error || error.message || errorMessage; |
| 282 | + console.error("API Error Response:", error); |
| 283 | + } catch (e) { |
| 284 | + const text = await response.text(); |
| 285 | + console.error("Non-JSON Error Response:", text); |
| 286 | + errorMessage = `Server error: ${response.status} ${response.statusText}`; |
| 287 | + } |
| 288 | + throw new Error(errorMessage); |
211 | 289 | } |
212 | 290 |
|
213 | 291 | const result = await response.json(); |
|
217 | 295 | alert("Record updated successfully"); |
218 | 296 | closeModals(); |
219 | 297 | } catch (error) { |
220 | | - alert(error instanceof Error ? error.message : "Failed to update record"); |
| 298 | + const errorMsg = |
| 299 | + error instanceof Error ? error.message : "Failed to update record"; |
| 300 | + alert(`Error: ${errorMsg}`); |
221 | 301 | console.error("Update error:", error); |
222 | 302 | } finally { |
223 | 303 | isSubmitting = false; |
|
234 | 314 | `/api/dynamic-entities/${entity.dynamicEntityId}/data/${record.id}`, |
235 | 315 | { |
236 | 316 | method: "DELETE", |
| 317 | + credentials: "include", |
237 | 318 | }, |
238 | 319 | ); |
239 | 320 |
|
|
260 | 341 | case "number": |
261 | 342 | return "number"; |
262 | 343 | case "DATE_WITH_DAY": |
263 | | - return "date"; |
| 344 | + return "text"; |
264 | 345 | default: |
265 | 346 | return "text"; |
266 | 347 | } |
|
605 | 686 | {fieldDef.description} |
606 | 687 | </p> |
607 | 688 | {/if} |
| 689 | + {#if fieldDef.type === "DATE_WITH_DAY"} |
| 690 | + <p class="mt-1 text-xs text-blue-600 dark:text-blue-400"> |
| 691 | + Format: YYYY-MM-DD (e.g., 2023-06-15) |
| 692 | + </p> |
| 693 | + {/if} |
608 | 694 | {#if inputType === "checkbox"} |
609 | 695 | <div class="mt-2"> |
610 | 696 | <input |
|
619 | 705 | type={inputType} |
620 | 706 | id="create-{fieldName}" |
621 | 707 | bind:value={formData[fieldName]} |
622 | | - placeholder={fieldDef.example ? String(fieldDef.example) : ""} |
623 | | - class="mt-2 block w-full rounded-lg border border-gray-300 bg-white px-3 py-2 text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-100" |
| 708 | + placeholder={fieldDef.type === "DATE_WITH_DAY" |
| 709 | + ? "YYYY-MM-DD" |
| 710 | + : fieldDef.example |
| 711 | + ? String(fieldDef.example) |
| 712 | + : ""} |
| 713 | + pattern={fieldDef.type === "DATE_WITH_DAY" |
| 714 | + ? "\\d{4}-\\d{2}-\\d{2}" |
| 715 | + : undefined} |
| 716 | + step={fieldDef.type === "integer" |
| 717 | + ? "1" |
| 718 | + : fieldDef.type === "number" |
| 719 | + ? "any" |
| 720 | + : undefined} |
| 721 | + min={fieldDef.minimum !== undefined |
| 722 | + ? fieldDef.minimum |
| 723 | + : undefined} |
| 724 | + max={fieldDef.maximum !== undefined |
| 725 | + ? fieldDef.maximum |
| 726 | + : undefined} |
| 727 | + class="mt-2 block w-full rounded-lg border border-gray-300 bg-white px-3 py-2 font-mono text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-100" |
624 | 728 | /> |
625 | 729 | {/if} |
626 | 730 | {#if validationErrors[fieldName]} |
|
715 | 819 | {fieldDef.description} |
716 | 820 | </p> |
717 | 821 | {/if} |
| 822 | + {#if fieldDef.type === "DATE_WITH_DAY"} |
| 823 | + <p class="mt-1 text-xs text-blue-600 dark:text-blue-400"> |
| 824 | + Format: YYYY-MM-DD (e.g., 2023-06-15) |
| 825 | + </p> |
| 826 | + {/if} |
718 | 827 | {#if inputType === "checkbox"} |
719 | 828 | <div class="mt-2"> |
720 | 829 | <input |
|
729 | 838 | type={inputType} |
730 | 839 | id="edit-{fieldName}" |
731 | 840 | bind:value={formData[fieldName]} |
732 | | - placeholder={fieldDef.example ? String(fieldDef.example) : ""} |
733 | | - class="mt-2 block w-full rounded-lg border border-gray-300 bg-white px-3 py-2 text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-100" |
| 841 | + placeholder={fieldDef.type === "DATE_WITH_DAY" |
| 842 | + ? "YYYY-MM-DD" |
| 843 | + : fieldDef.example |
| 844 | + ? String(fieldDef.example) |
| 845 | + : ""} |
| 846 | + pattern={fieldDef.type === "DATE_WITH_DAY" |
| 847 | + ? "\\d{4}-\\d{2}-\\d{2}" |
| 848 | + : undefined} |
| 849 | + step={fieldDef.type === "integer" |
| 850 | + ? "1" |
| 851 | + : fieldDef.type === "number" |
| 852 | + ? "any" |
| 853 | + : undefined} |
| 854 | + min={fieldDef.minimum !== undefined |
| 855 | + ? fieldDef.minimum |
| 856 | + : undefined} |
| 857 | + max={fieldDef.maximum !== undefined |
| 858 | + ? fieldDef.maximum |
| 859 | + : undefined} |
| 860 | + class="mt-2 block w-full rounded-lg border border-gray-300 bg-white px-3 py-2 font-mono text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-100" |
734 | 861 | /> |
735 | 862 | {/if} |
736 | 863 | {#if validationErrors[fieldName]} |
|
0 commit comments