CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Limiting Characters for custom fields
Pages: [1]

Author Topic: Limiting Characters for custom fields  (Read 686 times)

civicrmsav

  • I post frequently
  • ***
  • Posts: 121
  • Karma: 5
  • CiviCRM version: 4.2.6
  • CMS version: Drupal 7.x
Limiting Characters for custom fields
July 29, 2010, 02:05:40 pm
I've gotten this to work, but it's pretty much a hack and if someone would point me to a better way I would spend some time making this a better solution.

I needed to limit the number of characters in a text box that's used in a profile that's displayed when a member contributes.   I already have the functionality in drupal, using the maxlength module. 

So I in my custom /CRM/UF/Form/Block.tpl  I add ...

 
Code: [Select]
{if $form.$n.id eq 'custom_23'}
                       <script type="text/javascript" src="/sites/all/modules/maxlength/maxlength.js?T"></script>
                       {literal}<script type="text/javascript">jQuery.extend(Drupal.settings, { "maxlength": { "custom_23": "600" }});</script>{/literal}
                       <div id="maxlength-m_23" >Brief Intro limited to 600 characters, remaining: <strong><span class="maxlength-counter-remaining">600</span></strong></div>
                   {/if}
   

Then, since this only impacts the contribution page and not form where you can edit the membership, I make a similar change to /CRM/Custom/Form/CustomField.tpl with slightly different field names ...
 
Code: [Select]
   <script type="text/javascript" src="/sites/all/modules/maxlength/maxlength.js?T"></script>
   {literal}<script type="text/javascript">jQuery.extend(Drupal.settings, { "maxlength": { "custom_23_-1": "600" }});</script>{/literal}

and  later
Code: [Select]
    {if $element_name eq 'custom_23_-1'}
                       <div id="maxlength-m_23_-1" >Brief Intro limited to 600 characters, remaining: <strong><span class="maxlength-counter-remaining">600</span></strong></div>
                   {/if}

(the maxlength javascript replaces the first five chars of the textarea's id and with "maxlength" to get the id of the div where the remaining amount is.  I obviously could easily improve the javascript as part of this.)

Clearly this is an ugly solution for any number of reasons, like (1) its only good for drupal (2) you have to have maxlength installed, (3) you have to custom edit BOTH tpl files each time you want to use it.

So... any suggestions to point me in the right direction to make this a more full solution.   I'm very comfortable with php and javascript, but just getting my feet wet with civi templating let alone programming. 


lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
Re: Limiting Characters for custom fields
July 29, 2010, 02:28:12 pm
you could do it with the buildForm hook,
the addRule function
with the maxlength rule

often you restrict a hook to a specific form and ID. but in this case you could just add the rule universally
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Limiting Characters for custom fields

This forum was archived on 2017-11-26.