diff --git a/app/Controllers/Setting/Template.php b/app/Controllers/Setting/Template.php index c031e07..0a1d9ff 100644 --- a/app/Controllers/Setting/Template.php +++ b/app/Controllers/Setting/Template.php @@ -2836,6 +2836,24 @@ HTML; $isDefault = (int) ($this->request->getPost('is_default') ?? 0) === 1 ? 1 : 0; $status = (int) ($this->request->getPost('status') ?? 1) === 1 ? 1 : 0; + // In SPA/AJAX reload scenarios, CKEditor can occasionally post an empty + // template field even when editing an existing row. Preserve existing + // values for update requests instead of hard-failing. + if ($id > 0 && ($templateName === '' || trim($templateHtml) === '')) { + $existing = $this->db->table('ipd_discharge_templates') + ->select('template_name, template_html') + ->where('id', $id) + ->get(1) + ->getRowArray() ?? []; + + if ($templateName === '') { + $templateName = trim((string) ($existing['template_name'] ?? '')); + } + if (trim($templateHtml) === '') { + $templateHtml = (string) ($existing['template_html'] ?? ''); + } + } + if ($templateName === '' || trim($templateHtml) === '') { $notice = 'Template name and template HTML are required.'; $noticeType = 'danger'; diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index 190a052..4891b1a 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -519,13 +519,40 @@ $status = (int) ($edit['status'] ?? 1); if (CKEDITOR.instances && CKEDITOR.instances[editorFieldId]) { try { - CKEDITOR.instances[editorFieldId].updateElement(); + var instance = CKEDITOR.instances[editorFieldId]; + var htmlFromEditor = String(instance.getData ? instance.getData() : ''); + var templateHtmlEl = document.getElementById(editorFieldId); + if (templateHtmlEl && htmlFromEditor !== '') { + templateHtmlEl.value = htmlFromEditor; + } + instance.updateElement(); } catch (e) { console.warn('CKEditor updateElement failed:', e); } } } + function getTemplateHtmlValue() { + var html = ''; + + if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances && CKEDITOR.instances[editorFieldId]) { + try { + html = String(CKEDITOR.instances[editorFieldId].getData() || ''); + } catch (e) { + console.warn('CKEditor getData failed:', e); + } + } + + if (html === '') { + var templateHtmlEl = document.getElementById(editorFieldId); + if (templateHtmlEl) { + html = String(templateHtmlEl.value || ''); + } + } + + return html; + } + // Initialize editor after a short delay to ensure DOM is ready // This is especially important when loaded via AJAX if (document.readyState === 'loading') { @@ -547,7 +574,21 @@ $status = (int) ($edit['status'] ?? 1); e.preventDefault(); syncTemplateEditor(); + var htmlValue = getTemplateHtmlValue(); + var nameValue = fieldTemplateName ? String(fieldTemplateName.value || '').trim() : ''; + if (nameValue === '' || htmlValue.trim() === '') { + setNotice('danger', 'Template name and template HTML are required.'); + return; + } + + var templateHtmlEl = document.getElementById(editorFieldId); + if (templateHtmlEl) { + templateHtmlEl.value = htmlValue; + } + var formData = new FormData(form); + formData.set('template_name', nameValue); + formData.set('template_html', htmlValue); fetch(form.getAttribute('action'), { method: 'POST', body: formData,