Keyword

K2 Comments maxlength?

More
11 years 1 month ago #117512 by Max
K2 Comments maxlength? was created by Max
I've searched k2 parameters and googled it but didn't manage to find the solution, is it possible to limit the max length of the comments in k2 comments component? Thank you in advance!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
11 years 1 month ago #117513 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: K2 Comments maxlength?
Hello Max,

You can override the comment form and use a maxlength property on the textareas.
www.w3schools.com/tags/att_textarea_maxlength.asp (There are also several JS scripts).

More info on overriding can be found here: getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
11 years 1 month ago #117514 by Max
Replied by Max on topic Re: K2 Comments maxlength?
thanks for answer, i just wanted to make certain for myself, that there is no built in option in k2 comments parameters, which I was unable to find somehow

Please Log in or Create an account to join the conversation.

More
11 years 1 month ago #117515 by Max
Replied by Max on topic Re: K2 Comments maxlength?
for those who intrested I've did it in such way:
1. in the file \components\com_k2\models\item.php you need to find "function comment'. In this function you need to find string
$row->commentText = JRequest::getString('commentText', '', 'default');
now we can with mb_substr cut odd symbols adding right after the mentioned string new one:
$row->commentText = mb_substr($row->commentText, 0, 600);
as you can guess number 600 is the number of symbols which will leave in the $row->commentText, all other symbols will be cut. You can change this number to any you'd like.
2. then you can just add to the commentary description information about max symbols limitation in the comment, but we can do it in the more convinient for the end user way. depending on your template the necessary file could be in /templates/YOURJOOMLATEMPLATE/html/com_k2/item_comments_form.php if such folder exists. If not, then look for in /components/com_k2/templates/item_comments_form.php.
In this file we look for string
<form action="<?php echo JRoute::_('index.php'); ?>" method="post" id="comment-form" class="form-validate">
and insert in front of it
<script type="text/javascript">

function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    document.getElementById("message-text-counter").innerHTML = "Commentary cannot longer then 600 symbols";
    return false;
  }
  else
  {
    document.getElementById("message-text-counter").innerHTML = '<div id="message-text-counter"><span id="text-counter"></span> symbols left</div>';
    document.getElementById(countfield).innerHTML = maxlimit - field.value.length;
  }
}

	
</script>
you must change number 600 to the number of your choice
then the string
<textarea rows="20" cols="10" class="inputbox" onblur="if(this.value=='') this.value='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>';" onfocus="if(this.value=='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>') this.value='';" name="commentText" id="commentText"><?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?></textarea>
you change to
<textarea rows="20" cols="10" class="inputbox" onblur="if(this.value=='') this.value='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>';" onfocus="if(this.value=='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>') this.value='';" onkeyup="textCounter(this,'text-counter',600)" name="commentText" id="commentText"><?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?></textarea>
You see we added here
onkeyup="textCounter(this,'text-counter',600)"
here again you must change 600 to the number of symbols you have chosen in mb_substr function.
and as final stroke after
<div class="cform-item2">
			<label class="formComment" for="commentText"><?php echo JText::_('K2_MESSAGE'); ?> *</label>
			<textarea rows="20" cols="10" class="inputbox" onblur="if(this.value=='') this.value='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>';" onfocus="if(this.value=='<?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?>') this.value='';" onkeyup="textCounter(this,'text-counter',600)" name="commentText" id="commentText"><?php echo JText::_('K2_ENTER_YOUR_MESSAGE_HERE'); ?></textarea>
			
		</div>
you add
<div class="cform-item2" id="message-text-counter">
			<span id='text-counter'>600</span> symbols left</ br>
		</div>
here the same you must change 600 to your number.

Now user will see real-time symbols counter below comment textarea and this script will prevent user from enetering more then in my case 600 symbols even if user will copypaste ready text into the textarea. Even if user deactivate JavaScript and craete comment more then 600 symbols in my case, on the stage of processing comment the php script will cut off odd symbols in the text

IMPORTANT NOTICE
some attributes e.g. class can (and for sure WILL!!) be different depending on your template! So applying described changes you MUST change them to the values of your own template if you have any

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum