Hi, I've struck a problem with a custom comments form in my template.php.

The error I'm getting is: 'warning: implode() [function.implode]: Bad arguments. in /var/www/vhosts/drupalsnippets.com/httpdocs/includes/form.inc on line 621.'

Heres the code I'm using. Can you see any problems?

Note: The comment-theme-form.tpl simply contains: ' print drupal_render($form); ' with some html wrapped around it.

function phptemplate_comment_form($form) {
	$form['comment_filter']['format'] = array();
	$form['_author'] = array();
	$form['preview'] = array();
	$form['comment_filter']['comment'] = array(
		'#type' => 'textarea',
		'#title' => t('Add revisions, bug reports, feedback and questions for this snippet'),
		'#description' => t('Wrap any code in the appropriate language ‹tags›.'),
		'#rows' => 5,
		'#id' => 'edit-comment',
		'#name' => 'comment',
		'#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature,
		'#required' => TRUE
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Post comment'),
		'#id' => 'comment-submit',
		'#class' => 'form-submit',
		'#name' => 'op',
		'#weight' => 20
	);
	return _phptemplate_callback('comment-theme-form', array('form' => $form));
} 

Thanks for looking,
Marc

Comments

nevets’s picture

My guess it is these three lines

    $form['comment_filter']['format'] = array();
    $form['_author'] = array();
    $form['preview'] = array();

The simple approach would be to unset the fields instead (ex: unset($form['preview'] );)

But for 'format' and '_author' may need values (you would need to look at the submit code) in which case you want to use either a type of 'value' or 'hidden' with the value you want to be used.

Fiasst’s picture

Thanks for your reply, I've adjusted my code - shown below.
However, I've noticed the error disappears when I comment out the '$form['comment_filter']['comment']' (textarea) section of my code. I've tried to comment out each item of the array to try and find the trouble spot but it seems to be the entire textarea section that causes the error.

/* custom comment form */
function phptemplate_comment_form($form) {
	unset($form['comment_filter']['format']);
	$form['_author'] = array(
		'#type' => 'hidden',
		'#value' => $user->name
	);
	unset($form['preview']);
	$form['comment_filter']['format'] = array();
	$form['comment_filter']['comment'] = array(
		'#type' => 'textarea',
		'#title' => t('Add revisions, bug reports, feedback and questions for this snippet'),
		'#description' => t('Wrap any code in the appropriate language ‹tags›.'),
		'#rows' => 5,
		'#id' => 'edit-comment',
		'#name' => 'comment',
		'#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature,
		'#required' => TRUE
	);
	/*$form['comment_filter']['comment'] = array(
		'#type' => 'textarea',
		'#title' => t('Comment'),
		'#rows' => 15,
		'#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature,
		'#required' => TRUE
	);*/	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Post comment'),
		'#id' => 'comment-submit',
		'#class' => 'form-submit',
		'#name' => 'op',
		'#weight' => 20
	);
	return _phptemplate_callback('comment-theme-form', array('form' => $form));
}

Any ideas?

Thanks! :)

nsciacca’s picture

your warning implode is coming from these lines in form.inc lines 613-626

/**
 * Return the error message filed against the form with the specified name.
 */
function form_get_error($element) {
  $form = form_set_error();
  $key = $element['#parents'][0];
  if (isset($form[$key])) {
    return $form[$key];
  }
  $key = implode('][', $element['#parents']);
  if (isset($form[$key])) {
    return $form[$key];
  }
}

one of your elements has an error and when it goes to implode the #parents of the element it returns a warning because the #parents argument does not exist. I'm not sure what causes the form to trigger this error, but if you are sure your form is working correctly and just need to get rid of the message you can add the #parents param to your form elements it will eliminate the warning display.

ie..

 $form['comment_filter']['comment'] = array(
        '#type' => 'textarea',
        '#title' => t('Add revisions, bug reports, feedback and questions for this snippet'),
        '#description' => t('Wrap any code in the appropriate language ‹tags›.'),
        '#rows' => 5,
        '#id' => 'edit-comment',
        '#name' => 'comment',
        '#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature,
        '#required' => TRUE,
        '#parents' => []
    );

Without deviation from the norm, progress is not possible. - Frank Zappa

sarahara’s picture

hello,

i just found this and this is exactly what i need. but if i add the parents param to the template.php i get an syntax error in this line. what am i doing wrong? i just copied the code above.