I need to set maxlength based on some custom criteria. Is there a way I can set the maxlength of a given field programmatically in the theme or through FAPI? So say, the body field could have a different maxlength for the same field, and the same role, depending on other variables set programmatically. Any help here would be very much appreciated. I've spent a lot of time trying to figure it out. Thanks

Comments

techgirlgeek’s picture

I'd like this information also, if anyone has any insight. I only want the maxlength set for a specific role.

giorgio79’s picture

+1

If it would be as easy as for http://drupal.org/project/checkall that would be the best :)

With checkall this is all that is needed:

<?php
$form['foobar'] = array(
  '#type' => 'checkboxes',
  '#options' => $options,
  '#default_value' => $default_value,
  '#checkall' => TRUE,  // <--- this is it ;-)
);
?>

For maxlength we could have sg like

<?php
$form['foobar'] = array(
  '#type' => 'checkboxes',
  '#options' => $options,
  '#default_value' => $default_value,
  '#maxlength' => 128
  '#maxlength_countdown' => TRUE
  '#maxlength_message' => "too long"

);

?>

philpro’s picture

I needed to add this to just one comment form on one page, here was my solution — it basically mimics the functionality without all the mess. All we really need to populate to make the module take effect is to tell javascript files to load, pass in the js variables 'maxlength' and modify the form element

// Create a utility module, and tput this in it.
function modulename_form_alter(&$form, $form_state, $form_id) {

if($form_id == 'comment_form' && $form['nid']['#value'] == '2043' ) {

	$values['text'] = "";
	$values['limit'] = 315;
	$value = '';
	
	$id = 'comment';
	
	$path = drupal_get_path('module', 'maxlength');
    drupal_add_js($path .'/maxlength.js');
	$js_settings = array(
     	 'maxlength' => array(
      		  'edit-'. $id => $values['limit'],
     		 ),
   		 );
 	   drupal_add_js($js_settings, 'setting');
   	   
 	   // HARD CODED, CAUSE I COUDLN'T FIGURE OUT WHAT WAS BROKE
 	   $form['comment_filter']['#suffix'] = '<div class="maxlength-counter" id="maxlength-comment">Characters Remaining: <span class="maxlength-counter-remaining">315</span></div>';
 	   //$form['comment_filter']['#suffix'] = 
   	   //	'<div id="maxlength-'. $id .'" class="maxlength-counter">'. t($values['text'], array('!limit' => $values['limit'], '!count' =>'<span class="maxlength-count">'. drupal_strlen($value).'</span>', '!remaining' => '<span class="maxlength-counter-remaining">'. $remaining .'</span>')) .'</div>';
  		}
}
hefox’s picture

I plan to make a patch for #984794: Create API for non-node forms that should handle this situation also.

hmdnawaz’s picture

I used your code but It only displays the Characters remaining: 10 and when I wrote some text it does not decrement the value of Characters remaining.

hefox’s picture

1) Post comment in ticket with patch
2) Set that ticket to needs work
3) mention your browser, steps to reproduce, most importantly, any js errors noticed.

dawehner’s picture

Status: Active » Fixed

You can use now the api from hefox

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.