[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Error if the number of characters allowed is exceeded in the title and comment data field.

Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <691e4729829ba_2a17ef94707c@gitlab-sidekiq-low-urgency-cpu-bound-v2-6df8d84988-rdkzc.mail>

Benoit Grégoire pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
6d4af7a8 by NasserNgandu at 2025-11-19T22:31:52+00:00
[FIX] Error if the number of characters allowed is exceeded in the title and comment data field.
---
* [FIX] Code Correct.

* [FIX] Code Correct.

* [FIX] Code Correct.

* [FIX] Code correct.

* [FIX] Code Correct.

* [FIX] Code Correct.

* [FIX] Code Correct.

* [FIX] Pipeline correct.

* [FIX] Pipeline correct.

* [FIX] Pipeline correct.

* [FIX] Pipeline correct.

* [FIX] DRY correction.

* [FIX] Pipeline Correct.

* [FIX] Limit the number of characters in fields using jQuery.

* [FIX] Pipeline Correct.

* [FIX] Error if the number of characters allowed is exceeded in the title and comment data field.

See merge request tikiwiki/tiki!8811

- - - - -


9 changed files:

- installer/tiki-installer.php
- lib/comments/commentslib.php
- lib/core/Services/Comment/Controller.php
- lib/smarty_tiki/BlockHandler/TextArea.php
- path_js_importmap_generator.php
- + src/js/jquery-tiki/tiki-field_limiter.js
- templates/comment/edit.tpl
- templates/comment/post.tpl
- tiki-setup.php


Changes:

=====================================
installer/tiki-installer.php
=====================================
@@ -799,6 +799,7 @@ $headerlib->add_cssfile(FONTAWESOME_CSS_PATH . '/all.css');
 $headerlib->add_cssfile('themes/base_files/css/tiki_base.css');
 $headerlib->add_jsfile('lib/tiki-js.js');
 $headerlib->add_jsfile(JS_ASSETS_PATH . '/jquery-tiki/tiki-menu.js');
+$headerlib->add_js_module('import "@jquery-tiki/tiki-field_limiter";');
 $headerlib->add_jsfile_dependency(NODE_PUBLIC_DIST_PATH . "/jquery/dist/jquery.min.js");
 $headerlib->add_jsfile_dependency(NODE_PUBLIC_DIST_PATH . "/jquery-migrate/dist/jquery-migrate.min.js", true);
 $headerlib->add_jsfile_dependency(NODE_PUBLIC_DIST_PATH . "/jquery-ui/dist/jquery-ui.js");


=====================================
lib/comments/commentslib.php
=====================================
@@ -18,6 +18,8 @@ class Comments extends TikiLib
 {
     public $time_control = 0;
     private $extras = true;
+    public const MAX_COMMENT_TITLE_LENGTH = 255;
+    public const MAX_COMMENT_DATA_LENGTH = 65535;
 
     /* Functions for the forums */
     public function report_post($forumId, $parentId, $threadId, $user, $reason = '')


=====================================
lib/core/Services/Comment/Controller.php
=====================================
@@ -169,9 +169,13 @@ class Services_Comment_Controller
             if ($prefs['comments_notitle'] != 'y' && empty($title)) {
                 $errors['title'] = tr('Title is empty');
             }
-
+            if (mb_strlen($title) > Comments::MAX_COMMENT_TITLE_LENGTH) {
+                $errors['title'] = sprintf(tr("You have exceeded the number of characters allowed (%s max) for the comment title field"), Comments::MAX_COMMENT_TITLE_LENGTH);
+            }
             if (empty($data)) {
                 $errors['data'] = tr('Content is empty');
+            } elseif (mb_strlen($data) > Comments::MAX_COMMENT_DATA_LENGTH) {
+                $errors['data'] = sprintf(tr("You have exceeded the number of characters allowed (%s max) for the comment data field"), Comments::MAX_COMMENT_DATA_LENGTH);
             }
 
             if (empty($user) && $prefs['feature_antibot'] == 'y') {
@@ -329,6 +333,8 @@ class Services_Comment_Controller
             'type' => $type,
             'objectId' => $objectId,
             'title' => $title,
+            'max_comment_title_length' => Comments::MAX_COMMENT_TITLE_LENGTH,
+            'max_comment_data_length' => Comments::MAX_COMMENT_DATA_LENGTH,
             'data' => $data,
             'contributions' => $contributions,
             'anonymous_name' => $anonymous_name,
@@ -362,9 +368,13 @@ class Services_Comment_Controller
 
             $tikilib = TikiLib::lib('tiki');
             $data = $tikilib->convertAbsoluteLinksToRelative($data);
-
+            if (mb_strlen($title) > Comments::MAX_COMMENT_TITLE_LENGTH) {
+                $errors['title'] = sprintf(tr("You have exceeded the number of characters allowed (%s max) for the comment title field"), Comments::MAX_COMMENT_TITLE_LENGTH);
+            }
             if (empty($data)) {
                 $errors['data'] = tr('Content is empty');
+            } elseif (mb_strlen($data) > Comments::MAX_COMMENT_DATA_LENGTH) {
+                $errors['data'] = sprintf(tr("You have exceeded the number of characters allowed (%s max) for the comment data field"), Comments::MAX_COMMENT_DATA_LENGTH);
             }
 
             if (count($errors) === 0) {
@@ -382,6 +392,8 @@ class Services_Comment_Controller
 
         return [
             'comment' => $comment,
+            'max_comment_title_length' => Comments::MAX_COMMENT_TITLE_LENGTH,
+            'max_comment_data_length' => Comments::MAX_COMMENT_DATA_LENGTH,
             'diffInfo' => $diffInfo,
             'errors' => $errors,
             'type' => $comment['objectType'],


=====================================
lib/smarty_tiki/BlockHandler/TextArea.php
=====================================
@@ -71,7 +71,7 @@ class TextArea extends Base
         $params['name'] = $params['name'] ?? 'edit';
         $params['id'] = $params['id'] ?? 'editwiki';
         $params['area_id'] = $params['area_id'] ?? $params['id'];    // legacy param for toolbars?
-        $params['class'] = $params['class'] ?? 'wikiedit form-control';
+        $params['class'] = $params['class'] ?? 'wikiedit form-control check_character_limit';
         $params['comments'] = $params['comments'] ?? 'n';
         $params['autosave'] = $params['autosave'] ?? 'y';
 


=====================================
path_js_importmap_generator.php
=====================================
@@ -73,6 +73,7 @@ function generateJsImportmapScripts(bool $useBaseUrl = false)
                 "@jquery-tiki/eventcalendar_to_pdf" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/eventcalendar_to_pdf.js",
                 "@jquery-tiki/tiki-maps-ol3" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/tiki-maps-ol3.js",
                 "@jquery-tiki/tiki-password" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/tiki-password.js",
+                "@jquery-tiki/tiki-field_limiter" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/tiki-field_limiter.js",
                 "@jquery-tiki/timeago" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/timeago.js",
                 "@jquery-tiki/tracker-fields/emailFolder" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/tracker-fields/emailFolder.js",
                 "@jquery-tiki/tracker-fields/files" => $tikiUrl . JS_ASSETS_PATH . "/jquery-tiki/tracker-fields/files.js",


=====================================
src/js/jquery-tiki/tiki-field_limiter.js
=====================================
@@ -0,0 +1,18 @@
+$(function () {
+    $(document).on("keyup", ".check_character_limit", function () {
+        var maxChars = $(this).attr("maxlength");
+        if (typeof maxChars !== "undefined" && maxChars !== null) {
+            var inputLength = $(this).val().length;
+            var $messageElement = $("#" + $(this).attr("id") + "_message");
+            if ($messageElement.length === 0) {
+                $messageElement = $('<div id="' + $(this).attr("id") + '_message" style="color: red; display: none;padding: 5px;"></div>');
+                $(this).parent().append($messageElement);
+            }
+            if (inputLength > maxChars) {
+                $messageElement.text(tr("You have exceeded the number of characters allowed (" + maxChars + ") for this field")).show();
+            } else {
+                $messageElement.hide();
+            }
+        }
+    });
+});


=====================================
templates/comment/edit.tpl
=====================================
@@ -17,11 +17,11 @@
                 {if $prefs.comments_notitle neq 'y'}
                     <div class="mb-3 row">
                         <label for="comment-title" class="clearfix comment-title">{tr}Title{/tr}</label>
-                        <input type="text" id="comment-title" name="title" value="{$comment.title|escape}" class="form-control" placeholder="Comment title"/>
+                        <input type="text" id="comment-title" name="title" value="{$comment.title|escape}" class="form-control check_character_limit" placeholder="Comment title" maxlength="{$max_comment_title_length}"/>
                     </div>
                 {/if}
                 {capture name=rows}{if $type eq 'forum'}{$prefs.default_rows_textarea_forum}{else}{$prefs.default_rows_textarea_comment}{/if}{/capture}
-                {textarea codemirror='true' name=data comments="y" section=$type objectId=$objectId _wysiwyg="n" rows=$smarty.capture.rows _preview=$prefs.ajax_edit_previews}{$comment.data}{/textarea}
+                {textarea codemirror='true' name=data comments="y" maxlength="{$max_comment_data_length}" section=$type objectId=$objectId _wysiwyg="n" rows=$smarty.capture.rows _preview=$prefs.ajax_edit_previews}{$comment.data}{/textarea}
                 </div>
                 <div class="card-footer">
                     {if empty($comment.version)}


=====================================
templates/comment/post.tpl
=====================================
@@ -48,11 +48,11 @@
                     {if $prefs.comments_notitle neq 'y'}
                         <div class="mb-3">
                             <label for="comment-title" class="clearfix comment-title">{tr}Title{/tr}</label>
-                            <input type="text" id="comment-title" name="title" value="{$title|escape}" class="form-control" placeholder="Comment title"/>
+                            <input type="text" id="comment-title" name="title" value="{$title|escape}" class="form-control check_character_limit" placeholder="Comment title" maxlength="{$max_comment_title_length}"/>
                         </div>
                     {/if}
                     {capture name=rows}{if $type eq 'forum'}{$prefs.default_rows_textarea_forum}{else}{$prefs.default_rows_textarea_comment}{/if}{/capture}
-                    {textarea codemirror='true' name="data" comments="y" section=$type objectId=$objectId _wysiwyg="n" rows=$smarty.capture.rows class="form-control wikiedit" placeholder="{tr}Post new comment{/tr}..." _preview=$prefs.ajax_edit_previews}{$data|escape}{/textarea}
+                    {textarea codemirror='true' name="data" comments="y" maxlength="{$max_comment_data_length}" section=$type objectId=$objectId _wysiwyg="n" rows=$smarty.capture.rows class="form-control wikiedit check_character_limit" placeholder="{tr}Post new comment{/tr}..." _preview=$prefs.ajax_edit_previews}{$data|escape}{/textarea}
                     {if  $user and $prefs.feature_user_watches eq 'y'}
                         <div class="form-check">
                             <input id="watch_thread" type="checkbox" class="form-check-input" name="watch" value="y"{if $smarty.request.watch eq 'y'} checked="checked"{/if}>


=====================================
tiki-setup.php
=====================================
@@ -1086,6 +1086,8 @@ if ($prefs['feature_elementplus'] == 'y') {
 
 $headerlib->add_js_module('import "@jquery-tiki/constants";');
 $headerlib->add_js_module('import "@jquery-tiki/tiki-password";');
+// module allowing you to limit the number of characters allowed in the fields.
+$headerlib->add_js_module('import "@jquery-tiki/tiki-field_limiter";');
 
 $headerlib->add_cssfile(NODE_PUBLIC_DIST_PATH . '/summernote/dist/summernote-bs5.min.css');
 



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/6d4af7a8a6e72e9fbbbda821c745c09f54d61e77

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/6d4af7a8a6e72e9fbbbda821c745c09f54d61e77
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.