找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 49|回复: 1

[Discuz!论坛] Discuz! X5.0 允许只发标题(正文为空)的帖子的修改方法

[复制链接]
  • 打卡等级:本地老炮

9336

威望

6965

金钱

1万

贡献

管理员

自由的灵魂

积分
106908
主题
5610
回帖
26637
注册时间
2003-4-10
最后登录
2026-8-17
发表于 2026-7-10 11:44:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Discuz! X5.0 发新帖允许正文为空 - 完整修改方案

修改目的
允许用户在发新主题帖时,只填写标题,不填写任何正文内容,且不写入任何空 JSON 结构数据。




修改文件清单(共 4 个文件)

序号文件路径修改位置作用
1static/js/editorjs/init_content.jssaveJsonContent 函数前端编辑器层:允许空内容提交,且不写入 {"blocks":[]}
2static/js/forum_post.jsvalidate 函数前端表单层:删除正文为空的校验拦截
3source/app/forum/model/model_thread.phpnewthread 方法后端发帖模型:删除正文为空的校验拦截
4source/function/function_post.phpcheckpost 函数后端通用校验:删除正文最小长度检查





修改文件 1:static/js/editorjs/init_content.js

查找saveJsonContent 函数(约第 186-200 行)

原始代码
  1. function saveJsonContent(event) {
  2.         editor.save()
  3.             .then((savedData) => {
  4.                     //console.log(JSON.stringify(savedData, null, 4));
  5.                     var postform = document.getElementById("postform");

  6.                     var content = document.getElementById("content");
  7.                     if (savedData.blocks == '' || savedData.blocks == undefined) {
  8.                             editor.notifier.show({
  9.                                     message: $L("json_editor_tip_content_null"),
  10.                                     style: 'error',
  11.                                     // time: 30
  12.                             });
  13.                             event.stopPropagation();
  14.                             return false;
  15.                     }
  16.                     content.value = JSON.stringify(savedData);
  17.                     //console.log(content.value);

  18.             }).then(() => {
  19.                 ajaxpost('postform', 'return_postform', 'return_postform', 'onerror');
  20.         })
  21.             .catch((error) => {
  22.                     console.error($L("json_editor_tip_content_null"), error);
  23.             });
  24. }
复制代码


修改后代码
  1. function saveJsonContent(event) {
  2.         // ================================================================
  3.         // 修改目的:移除正文为空时的拦截,允许提交只有标题的帖子
  4.         // 修改说明:删除 blocks 为空的检测逻辑,空内容时写入空字符串 ''
  5.         // ================================================================
  6.         editor.save()
  7.             .then((savedData) => {
  8.                     var content = document.getElementById("content");
  9.                     
  10.                     // ============================================================
  11.                     // 修改点:有内容时写入 JSON,无内容时写入空字符串
  12.                     // 原代码:content.value = JSON.stringify(savedData);
  13.                     // 问题:空内容时会写入 {"blocks":[]},不是真正的空
  14.                     // 修改后:完全为空时写入 '',数据库存储为空字符串
  15.                     // ============================================================
  16.                     if (savedData.blocks && savedData.blocks.length > 0) {
  17.                         // 有正文内容:正常写入 JSON 数据
  18.                         content.value = JSON.stringify(savedData);
  19.                     } else {
  20.                         // 无正文内容:写入空字符串,实现正文字段完全为空
  21.                         content.value = '';
  22.                     }
  23.                     // ============================================================

  24.             }).then(() => {
  25.                 // 提交表单
  26.                 ajaxpost('postform', 'return_postform', 'return_postform', 'onerror');
  27.         })
  28.             .catch((error) => {
  29.                     console.error($L("json_editor_tip_content_null"), error);
  30.             });
  31.         // ================================================================
  32. }
复制代码





修改文件 2:static/js/forum_post.js

查找validate 函数(约第 63-80 行)

原始代码
  1. function validate(theform) {
  2.         var message = wysiwyg ? html2bbcode(getEditorContents()) : theform.message.value;
  3.         if(!theform.parseurloff.checked) {
  4.                 message = parseurl(message);
  5.         }
  6.         if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
  7.                 showError($L('subject_empty'));
  8.                 return false;
  9.         } else if(dstrlen(theform.subject.value) > 255) {
  10.                 showError($L('subject_length_limit', [255]));
  11.                 return false;
  12.         }
  13.         // ... 后续代码
  14. }
复制代码


修改后代码
  1. function validate(theform) {
  2.         // ================================================================
  3.         // 修改目的:允许只发标题(正文为空)的帖子
  4.         // 修改说明:删除正文为空的检查条件,只保留标题为空的检查
  5.         // ================================================================
  6.         var message = wysiwyg ? html2bbcode(getEditorContents()) : theform.message.value;
  7.         if(!theform.parseurloff.checked) {
  8.                 message = parseurl(message);
  9.         }
  10.        
  11.         // ============ 修改点:只检查标题是否为空 ============
  12.         // 原代码:标题为空 或 (非特殊帖且正文为空) → 报错
  13.         // 修改后:仅标题为空 → 报错,正文可以为空
  14.         // ===================================================
  15.         if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "")) {
  16.                 showError($L('subject_empty'));
  17.                 return false;
  18.         }
  19.         // ============ 修改点结束 ============================
  20.        
  21.         else if(dstrlen(theform.subject.value) > 255) {
  22.                 showError($L('subject_length_limit', [255]));
  23.                 return false;
  24.         }
  25.         // ... 后续代码保持不变
  26. }
复制代码





修改文件 3:source/app/forum/model/model_thread.php

查找newthread 方法中的正文检查(约第 55-68 行)

原始代码
  1. if(trim($this->param['subject']) == '') {
  2.     return $this->showmessage('post_sm_isnull');
  3. }

  4. if(!$this->param['sortid'] && (!$this->setting['json_independence'] && !$this->param['special']) && ((in_array($this->param['contentType'], ['text', '']) && empty(trim($this->param['message']))) || (!empty($this->param['contentType']) && $this->param['contentType'] != 'text' && empty(trim($this->param['content']))))) {
  5.     return $this->showmessage('post_sm_isnull');
  6. }
复制代码


修改后代码
  1. // ================================================================
  2. // 修改目的:允许只发标题(正文为空)的帖子
  3. // 修改日期:2026-07-11
  4. // ================================================================

  5. // ============ 标题检查(保留) ============
  6. if(trim($this->param['subject']) == '') {
  7.     return $this->showmessage('post_sm_isnull');
  8. }
  9. // ============ 标题检查结束 ================

  10. // ============ 修改点:彻底注释正文为空检查 ============
  11. // 原代码:检测到正文为空时返回 'post_sm_isnull'
  12. // 注释后:允许正文为空,只检查标题
  13. // =========================================================
  14. // if(!$this->param['sortid'] && (!$this->setting['json_independence'] && !$this->param['special']) && ((in_array($this->param['contentType'], ['text', '']) && empty(trim($this->param['message']))) || (!empty($this->param['contentType']) && $this->param['contentType'] != 'text' && empty(trim($this->param['content']))))) {
  15. //     return $this->showmessage('post_sm_isnull');
  16. // }
  17. // ============ 修改点结束 ================================
复制代码





修改文件 4:source/function/function_post.php

查找checkpost 函数(约第 224-249 行)

原始代码
  1. function checkpost($subject, $message, $special = 0, $isJson = false) {
  2.         global $_G;
  3.         if(dstrlen($subject) > 255) {
  4.                 return 'post_subject_toolong';
  5.         }
  6.         if(!$_G['group']['disablepostctrl'] && !$special && !$isJson) {
  7.                 if($_G['setting']['maxpostsize'] && strlen($message) > $_G['setting']['maxpostsize']) {
  8.                         return 'post_message_toolong';
  9.                 } elseif($_G['setting']['minpostsize']) {
  10.                         $minpostsize = !defined('IN_MOBILE') || !constant('IN_MOBILE') || !$_G['setting']['minpostsize_mobile'] ? $_G['setting']['minpostsize'] : $_G['setting']['minpostsize_mobile'];
  11.                         if(strlen(preg_replace('/\[quote\].+?\[\/quote\]/is', '', $message)) < $minpostsize || strlen(preg_replace('/\[postbg\].+?\[\/postbg\]/is', '', $message)) < $minpostsize) {
  12.                                 return 'post_message_tooshort';
  13.                         }
  14.                 }
  15.                 if($_G['setting']['maxsubjectsize'] && dstrlen($subject) > $_G['setting']['maxsubjectsize']) {
  16.                         return 'post_subject_toolong';
  17.                 } elseif(dstrlen($subject) && $_G['setting']['minsubjectsize'] && dstrlen($subject) < $_G['setting']['minsubjectsize']) {
  18.                         return 'post_subject_tooshort';
  19.                 }
  20.         }
  21.         return FALSE;
  22. }
复制代码


修改后代码
  1. function checkpost($subject, $message, $special = 0, $isJson = false) {
  2.         // ================================================================
  3.         // 修改目的:允许正文为空,只保留标题校验
  4.         // 修改说明:注释掉正文最小长度(minpostsize)的检查逻辑
  5.         // 注意:标题的长度检查仍然保留,确保标题符合要求
  6.         // ================================================================
  7.         global $_G;
  8.         if(dstrlen($subject) > 255) {
  9.                 return 'post_subject_toolong';
  10.         }
  11.         if(!$_G['group']['disablepostctrl'] && !$special && !$isJson) {
  12.                 if($_G['setting']['maxpostsize'] && strlen($message) > $_G['setting']['maxpostsize']) {
  13.                         return 'post_message_toolong';
  14.                 // ============================================================
  15.                 // 修改点:注释掉正文最小长度检查
  16.                 // 原代码:检测到正文长度小于 minpostsize 时返回 'post_message_tooshort'
  17.                 // 注释后:空正文也能通过长度校验
  18.                 // ============================================================
  19.                 // } elseif($_G['setting']['minpostsize']) {
  20.                 //         $minpostsize = !defined('IN_MOBILE') || !constant('IN_MOBILE') || !$_G['setting']['minpostsize_mobile'] ? $_G['setting']['minpostsize'] : $_G['setting']['minpostsize_mobile'];
  21.                 //         if(strlen(preg_replace('/\[quote\].+?\[\/quote\]/is', '', $message)) < $minpostsize || strlen(preg_replace('/\[postbg\].+?\[\/postbg\]/is', '', $message)) < $minpostsize) {
  22.                 //                 return 'post_message_tooshort';
  23.                 //         }
  24.                 // ============================================================
  25.                 }
  26.                 // ============================================================
  27.                 // 标题长度检查(保留)
  28.                 // ============================================================
  29.                 if($_G['setting']['maxsubjectsize'] && dstrlen($subject) > $_G['setting']['maxsubjectsize']) {
  30.                         return 'post_subject_toolong';
  31.                 } elseif(dstrlen($subject) && $_G['setting']['minsubjectsize'] && dstrlen($subject) < $_G['setting']['minsubjectsize']) {
  32.                         return 'post_subject_tooshort';
  33.                 }
  34.         }
  35.         return FALSE;
  36. }
复制代码





修改原理说明

检查层级文件检查内容修改方式
前端-编辑器层init_content.js编辑器保存时检测 blocks 是否为空删除检测逻辑
前端-表单层forum_post.js提交表单时检测 message 是否为空删除检测条件
后端-业务层model_thread.php发帖时检测 message/content 是否为空注释检测代码
后端-通用层function_post.phpcheckpost 函数检测内容最小长度注释 minpostsize 检查





操作注意事项

  • 备份原文件:修改前请先备份所有要修改的文件
  • 清除浏览器缓存:修改 JS 文件后需强制刷新(Ctrl+F5)或清除浏览器缓存
  • 清理系统缓存:Discuz 后台 → 工具 → 更新缓存
  • 测试验证:用测试账号发一个只有标题的帖子,确认能正常发布

不懂就搜!点此搜点拨论坛。如果本坛没有,请尝试点此问AI,或跟帖提问。
发帖前注意看置顶帖
不定期借助AI对点拨论坛陈年老帖进行挖坟回复,打扰勿怪!
有问题请直接提问,欢迎注册本论坛!
  • 打卡等级:本地老炮

9336

威望

6965

金钱

1万

贡献

管理员

自由的灵魂

积分
106908
主题
5610
回帖
26637
注册时间
2003-4-10
最后登录
2026-8-17
 楼主| 发表于 2026-7-13 11:17:48 | 显示全部楼层
Discuz! X5.0 手机版发新帖允许正文为空 - 完整修改方案

修改目的
允许用户在手机版发新主题帖时,只填写标题,不填写任何正文内容,且不写入任何空 JSON 结构数据。




手机版修改文件清单(共 3 处修改)

序号文件路径修改位置作用
1template/default/touch/forum/post.phpbtn.on('click') 事件中的校验删除正文为空的提交拦截
2template/default/touch/forum/post.phpupdateButton() 函数按钮启用只检查标题,不检查正文
3template/default/touch/forum/post.phpcheckMessage() 函数正文状态始终为 true,不影响按钮状态





修改文件:template/default/touch/forum/post.php

找到 JS 代码区域(约第 230-310 行),按以下三处修改:

修改点 1:btn.on('click') 提交校验

查找:约第 250-270 行的按钮点击事件

原始代码
  1. savePromise.then(function(savedData) {
  2.     if (_content && _content.value) {
  3.         post_content = _content.value;
  4.     }

  5.     <!--{if $postinfo['first']}-->
  6.     if (!_needsubject || $.trim(_needsubject.value) === '' || $.trim(post_content) === '') {
  7.         popup.open('{lang post_sm_isnull}', 'alert');
  8.         return false;
  9.     }
  10.     <!--{/if}-->

  11.     continueSubmit();
  12. })
复制代码


修改后代码
  1. savePromise.then(function(savedData) {
  2.     if (_content && _content.value) {
  3.         post_content = _content.value;
  4.     }

  5.     <!--{if $postinfo['first']}-->
  6.     // ================================================================
  7.     // 修改点 1:允许只发标题(正文为空)的帖子
  8.     // 修改说明:只检查标题是否为空,不再检查正文是否为空
  9.     // 修改前:if (!_needsubject || $.trim(_needsubject.value) === '' || $.trim(post_content) === '')
  10.     // 修改后:只检查标题
  11.     // ================================================================
  12.     if (!_needsubject || $.trim(_needsubject.value) === '') {
  13.         popup.open('{lang post_sm_isnull}', 'alert');
  14.         return false;
  15.     }
  16.     // ================================================================
  17.     <!--{/if}-->

  18.     continueSubmit();
  19. })
复制代码


修改点 2:updateButton() 按钮启用逻辑

查找:约第 105-115 行的 updateButton 函数

原始代码
  1. function updateButton() {
  2.     if (state.submitting) {
  3.         btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  4.         return;
  5.     }
  6.     if (state.needsubject && state.needmessage) {
  7.         btn.attr('data-disabled', 'false').removeClass('btn_pn_grey').addClass('btn_pn_blue');
  8.     } else {
  9.         btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  10.     }
  11. }
复制代码


修改后代码
  1. // ================================================================
  2. // 修改点 2:按钮启用只检查标题
  3. // 修改说明:删除正文检查,只需标题有值即可启用按钮
  4. // 修改前:if (state.needsubject && state.needmessage)
  5. // 修改后:只检查标题是否有值
  6. // ================================================================
  7. function updateButton() {
  8.     if (state.submitting) {
  9.         btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  10.         return;
  11.     }
  12.     if (state.needsubject) {
  13.         btn.attr('data-disabled', 'false').removeClass('btn_pn_grey').addClass('btn_pn_blue');
  14.     } else {
  15.         btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  16.     }
  17. }
  18. // ================================================================
复制代码


修改点 3:checkMessage() 正文状态

查找:约第 125-128 行的 checkMessage 函数

原始代码
  1. function checkMessage() {
  2.     state.needmessage = !isEmpty(needmessage);
  3.     updateButton();
  4. }
复制代码


修改后代码
  1. // ================================================================
  2. // 修改点 3:正文状态始终为 true
  3. // 修改说明:正文不再影响按钮启用状态
  4. // 修改前:state.needmessage = !isEmpty(needmessage);
  5. // 修改后:始终认为正文有值
  6. // ================================================================
  7. function checkMessage() {
  8.     state.needmessage = true;
  9.     updateButton();
  10. }
  11. // ================================================================
复制代码


完整修改后的 JS 代码段(供参考)

  1. (function($) {
  2.     'use strict';

  3.     var btn = $('#postsubmit'),
  4.         form = $('#postform'),
  5.         needsubject = $('#needsubject'),
  6.         needmessage = $('#needmessage');

  7.     var state = {
  8.         needsubject: false,
  9.         needmessage: false,
  10.         submitting: false
  11.     };

  12.     <!--{if $_GET['action'] == 'reply'}-->
  13.     state.needsubject = true;
  14.     <!--{elseif $_GET['action'] == 'edit'}-->
  15.     state.needsubject = true;
  16.     state.needmessage = true;
  17.     <!--{/if}-->

  18.     // ================================================================
  19.     // 修改点 2:按钮启用只检查标题
  20.     // ================================================================
  21.     function updateButton() {
  22.         if (state.submitting) {
  23.             btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  24.             return;
  25.         }
  26.         if (state.needsubject) {
  27.             btn.attr('data-disabled', 'false').removeClass('btn_pn_grey').addClass('btn_pn_blue');
  28.         } else {
  29.             btn.attr('data-disabled', 'true').removeClass('btn_pn_blue').addClass('btn_pn_grey');
  30.         }
  31.     }

  32.     function isEmpty(el) {
  33.         return !el.length || $.trim(el.val() || '') === '';
  34.     }

  35.     var subjectTimer = null,
  36.         messageTimer = null;

  37.     function checkSubject() {
  38.         state.needsubject = !isEmpty(needsubject);
  39.         updateButton();
  40.     }

  41.     // ================================================================
  42.     // 修改点 3:正文状态始终为 true
  43.     // ================================================================
  44.     function checkMessage() {
  45.         state.needmessage = true;
  46.         updateButton();
  47.     }

  48.     <!--{if $_GET['action'] == 'newthread' || ($_GET['action'] == 'edit' && $isfirstpost)}-->
  49.     needsubject.on('keyup input', function() {
  50.         clearTimeout(subjectTimer);
  51.         subjectTimer = setTimeout(checkSubject, 150);
  52.     });
  53.     <!--{/if}-->

  54.     needmessage.on('keyup input', function() {
  55.         clearTimeout(messageTimer);
  56.         messageTimer = setTimeout(checkMessage, 150);
  57.     });

  58.     updateButton();

  59.     btn.on('click', function(e) {
  60.         e.preventDefault();

  61.         if (btn.attr('data-disabled') === 'true' || state.submitting) {
  62.             return false;
  63.         }

  64.         <!--{if (!empty($_G['setting']['editormodetype']) && $_GET['action'] != 'edit') || ($_GET['action'] == 'edit' && $isJsonContent)}-->
  65.         var _needsubject = document.getElementById('needsubject');
  66.         var _content = document.getElementById('content');
  67.         var post_content = '';
  68.         <!--{if $_GET['action'] == 'edit' && $isJsonContent}-->
  69.         post_content = `{$postinfo['content']}`;
  70.         <!--{/if}-->

  71.         var savePromise = (typeof saveJsonContent === 'function')
  72.             ? saveJsonContent()
  73.             : Promise.resolve();

  74.         savePromise.then(function(savedData) {
  75.             if (_content && _content.value) {
  76.                 post_content = _content.value;
  77.             }

  78.             <!--{if $postinfo['first']}-->
  79.             // ================================================================
  80.             // 修改点 1:只检查标题
  81.             // ================================================================
  82.             if (!_needsubject || $.trim(_needsubject.value) === '') {
  83.                 popup.open('{lang post_sm_isnull}', 'alert');
  84.                 return false;
  85.             }
  86.             // ================================================================
  87.             <!--{/if}-->

  88.             continueSubmit();
  89.         }).catch(function(error) {
  90.             console.error('保存编辑器内容失败:', error);
  91.             popup.open('{lang networkerror}', 'alert');
  92.             return false;
  93.         });

  94.         return false;
  95.         <!--{else}-->
  96.         continueSubmit();
  97.         <!--{/if}-->
  98.     });

  99.     function continueSubmit() {
  100.         // ... 保持不变
  101.     }
  102. })(jQuery);
复制代码


手机版修改汇总

序号修改位置修改前修改后
1btn.on('click') 校验标题为空 正文为空 → 报错仅标题为空 → 报错
2updateButton() 启用条件state.needsubject && state.needmessagestate.needsubject
3checkMessage() 赋值state.needmessage = !isEmpty(needmessage)state.needmessage = true


操作注意事项

  • 备份原文件后再修改
  • 模板缓存更新:修改后需在 Discuz 后台更新模板缓存(工具 → 更新缓存 → 模板缓存)
  • 浏览器缓存:手机浏览器需要清除缓存或强制刷新
  • 测试验证:用测试账号发一个只有标题的帖子,确认能正常发布

不懂就搜!点此搜点拨论坛。如果本坛没有,请尝试点此问AI,或跟帖提问。
发帖前注意看置顶帖
不定期借助AI对点拨论坛陈年老帖进行挖坟回复,打扰勿怪!
点拨网 — 致力于解决实际问题!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|点拨论坛 |网站地图|网站地图

GMT+8, 2026-8-17 10:09 , Processed in 0.031885 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表