活力39183
在线时间13465 小时
阅读权限200
管理员
自由的灵魂
- 积分
- 106908
- 主题
- 5610
- 回帖
- 26637
- 注册时间
- 2003-4-10
- 最后登录
- 2026-8-17
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Discuz! X5.0 发新帖允许正文为空 - 完整修改方案
修改目的
允许用户在发新主题帖时,只填写标题,不填写任何正文内容,且不写入任何空 JSON 结构数据。
修改文件清单(共 4 个文件)
| 序号 | 文件路径 | 修改位置 | 作用 | | 1 | static/js/editorjs/init_content.js | saveJsonContent 函数 | 前端编辑器层:允许空内容提交,且不写入 {"blocks":[]} | | 2 | static/js/forum_post.js | validate 函数 | 前端表单层:删除正文为空的校验拦截 | | 3 | source/app/forum/model/model_thread.php | newthread 方法 | 后端发帖模型:删除正文为空的校验拦截 | | 4 | source/function/function_post.php | checkpost 函数 | 后端通用校验:删除正文最小长度检查 |
修改文件 1:static/js/editorjs/init_content.js
查找:saveJsonContent 函数(约第 186-200 行)
原始代码:
- function saveJsonContent(event) {
- editor.save()
- .then((savedData) => {
- //console.log(JSON.stringify(savedData, null, 4));
- var postform = document.getElementById("postform");
- var content = document.getElementById("content");
- if (savedData.blocks == '' || savedData.blocks == undefined) {
- editor.notifier.show({
- message: $L("json_editor_tip_content_null"),
- style: 'error',
- // time: 30
- });
- event.stopPropagation();
- return false;
- }
- content.value = JSON.stringify(savedData);
- //console.log(content.value);
- }).then(() => {
- ajaxpost('postform', 'return_postform', 'return_postform', 'onerror');
- })
- .catch((error) => {
- console.error($L("json_editor_tip_content_null"), error);
- });
- }
复制代码
修改后代码:
- function saveJsonContent(event) {
- // ================================================================
- // 修改目的:移除正文为空时的拦截,允许提交只有标题的帖子
- // 修改说明:删除 blocks 为空的检测逻辑,空内容时写入空字符串 ''
- // ================================================================
- editor.save()
- .then((savedData) => {
- var content = document.getElementById("content");
-
- // ============================================================
- // 修改点:有内容时写入 JSON,无内容时写入空字符串
- // 原代码:content.value = JSON.stringify(savedData);
- // 问题:空内容时会写入 {"blocks":[]},不是真正的空
- // 修改后:完全为空时写入 '',数据库存储为空字符串
- // ============================================================
- if (savedData.blocks && savedData.blocks.length > 0) {
- // 有正文内容:正常写入 JSON 数据
- content.value = JSON.stringify(savedData);
- } else {
- // 无正文内容:写入空字符串,实现正文字段完全为空
- content.value = '';
- }
- // ============================================================
- }).then(() => {
- // 提交表单
- ajaxpost('postform', 'return_postform', 'return_postform', 'onerror');
- })
- .catch((error) => {
- console.error($L("json_editor_tip_content_null"), error);
- });
- // ================================================================
- }
复制代码
修改文件 2:static/js/forum_post.js
查找:validate 函数(约第 63-80 行)
原始代码:
- function validate(theform) {
- var message = wysiwyg ? html2bbcode(getEditorContents()) : theform.message.value;
- if(!theform.parseurloff.checked) {
- message = parseurl(message);
- }
- if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
- showError($L('subject_empty'));
- return false;
- } else if(dstrlen(theform.subject.value) > 255) {
- showError($L('subject_length_limit', [255]));
- return false;
- }
- // ... 后续代码
- }
复制代码
修改后代码:
- function validate(theform) {
- // ================================================================
- // 修改目的:允许只发标题(正文为空)的帖子
- // 修改说明:删除正文为空的检查条件,只保留标题为空的检查
- // ================================================================
- var message = wysiwyg ? html2bbcode(getEditorContents()) : theform.message.value;
- if(!theform.parseurloff.checked) {
- message = parseurl(message);
- }
-
- // ============ 修改点:只检查标题是否为空 ============
- // 原代码:标题为空 或 (非特殊帖且正文为空) → 报错
- // 修改后:仅标题为空 → 报错,正文可以为空
- // ===================================================
- if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "")) {
- showError($L('subject_empty'));
- return false;
- }
- // ============ 修改点结束 ============================
-
- else if(dstrlen(theform.subject.value) > 255) {
- showError($L('subject_length_limit', [255]));
- return false;
- }
- // ... 后续代码保持不变
- }
复制代码
修改文件 3:source/app/forum/model/model_thread.php
查找:newthread 方法中的正文检查(约第 55-68 行)
原始代码:
- if(trim($this->param['subject']) == '') {
- return $this->showmessage('post_sm_isnull');
- }
- 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']))))) {
- return $this->showmessage('post_sm_isnull');
- }
复制代码
修改后代码:
- // ================================================================
- // 修改目的:允许只发标题(正文为空)的帖子
- // 修改日期:2026-07-11
- // ================================================================
- // ============ 标题检查(保留) ============
- if(trim($this->param['subject']) == '') {
- return $this->showmessage('post_sm_isnull');
- }
- // ============ 标题检查结束 ================
- // ============ 修改点:彻底注释正文为空检查 ============
- // 原代码:检测到正文为空时返回 'post_sm_isnull'
- // 注释后:允许正文为空,只检查标题
- // =========================================================
- // 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']))))) {
- // return $this->showmessage('post_sm_isnull');
- // }
- // ============ 修改点结束 ================================
复制代码
修改文件 4:source/function/function_post.php
查找:checkpost 函数(约第 224-249 行)
原始代码:
- function checkpost($subject, $message, $special = 0, $isJson = false) {
- global $_G;
- if(dstrlen($subject) > 255) {
- return 'post_subject_toolong';
- }
- if(!$_G['group']['disablepostctrl'] && !$special && !$isJson) {
- if($_G['setting']['maxpostsize'] && strlen($message) > $_G['setting']['maxpostsize']) {
- return 'post_message_toolong';
- } elseif($_G['setting']['minpostsize']) {
- $minpostsize = !defined('IN_MOBILE') || !constant('IN_MOBILE') || !$_G['setting']['minpostsize_mobile'] ? $_G['setting']['minpostsize'] : $_G['setting']['minpostsize_mobile'];
- if(strlen(preg_replace('/\[quote\].+?\[\/quote\]/is', '', $message)) < $minpostsize || strlen(preg_replace('/\[postbg\].+?\[\/postbg\]/is', '', $message)) < $minpostsize) {
- return 'post_message_tooshort';
- }
- }
- if($_G['setting']['maxsubjectsize'] && dstrlen($subject) > $_G['setting']['maxsubjectsize']) {
- return 'post_subject_toolong';
- } elseif(dstrlen($subject) && $_G['setting']['minsubjectsize'] && dstrlen($subject) < $_G['setting']['minsubjectsize']) {
- return 'post_subject_tooshort';
- }
- }
- return FALSE;
- }
复制代码
修改后代码:
- function checkpost($subject, $message, $special = 0, $isJson = false) {
- // ================================================================
- // 修改目的:允许正文为空,只保留标题校验
- // 修改说明:注释掉正文最小长度(minpostsize)的检查逻辑
- // 注意:标题的长度检查仍然保留,确保标题符合要求
- // ================================================================
- global $_G;
- if(dstrlen($subject) > 255) {
- return 'post_subject_toolong';
- }
- if(!$_G['group']['disablepostctrl'] && !$special && !$isJson) {
- if($_G['setting']['maxpostsize'] && strlen($message) > $_G['setting']['maxpostsize']) {
- return 'post_message_toolong';
- // ============================================================
- // 修改点:注释掉正文最小长度检查
- // 原代码:检测到正文长度小于 minpostsize 时返回 'post_message_tooshort'
- // 注释后:空正文也能通过长度校验
- // ============================================================
- // } elseif($_G['setting']['minpostsize']) {
- // $minpostsize = !defined('IN_MOBILE') || !constant('IN_MOBILE') || !$_G['setting']['minpostsize_mobile'] ? $_G['setting']['minpostsize'] : $_G['setting']['minpostsize_mobile'];
- // if(strlen(preg_replace('/\[quote\].+?\[\/quote\]/is', '', $message)) < $minpostsize || strlen(preg_replace('/\[postbg\].+?\[\/postbg\]/is', '', $message)) < $minpostsize) {
- // return 'post_message_tooshort';
- // }
- // ============================================================
- }
- // ============================================================
- // 标题长度检查(保留)
- // ============================================================
- if($_G['setting']['maxsubjectsize'] && dstrlen($subject) > $_G['setting']['maxsubjectsize']) {
- return 'post_subject_toolong';
- } elseif(dstrlen($subject) && $_G['setting']['minsubjectsize'] && dstrlen($subject) < $_G['setting']['minsubjectsize']) {
- return 'post_subject_tooshort';
- }
- }
- return FALSE;
- }
复制代码
修改原理说明
| 检查层级 | 文件 | 检查内容 | 修改方式 | | 前端-编辑器层 | init_content.js | 编辑器保存时检测 blocks 是否为空 | 删除检测逻辑 | | 前端-表单层 | forum_post.js | 提交表单时检测 message 是否为空 | 删除检测条件 | | 后端-业务层 | model_thread.php | 发帖时检测 message/content 是否为空 | 注释检测代码 | | 后端-通用层 | function_post.php | checkpost 函数检测内容最小长度 | 注释 minpostsize 检查 |
操作注意事项
- 备份原文件:修改前请先备份所有要修改的文件
- 清除浏览器缓存:修改 JS 文件后需强制刷新(Ctrl+F5)或清除浏览器缓存
- 清理系统缓存:Discuz 后台 → 工具 → 更新缓存
- 测试验证:用测试账号发一个只有标题的帖子,确认能正常发布
|
|