fix(discharge-template): prevent empty template payload on ajax save
This commit is contained in:
parent
d7e5f16f79
commit
49a2027778
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user