I'm looking at limiting the number of file attachments to maybe 2 or 3 files only. Is this possible in anyway?

Thanks.

Comments

hanifnorman’s picture

Can I change this to critical because my boss wants this done by yesterday?

heine’s picture

No. Your boss is your problem ;)

You need to add some code to the AHAH callback and form validation handlers.

hanifnorman’s picture

I created a custom validation method in my template.php file and then added the function name to #validate in the $form array. I'm not really sure if I did correctly but it isn't working. I'm also not familiar with AHAH callbacks. I'll read up on this but here's the code for the validation.

function mytheme_comment_upload_form_validate($form, &$form_state){
	if (empty($form_state['values']['op']) && (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] != 'Attach')) {
	    return;
	} elseif(!empty($form_state['rebuild'])) {
	    return;
	}
	
	$count = 0;
	
	// Count number of files
	foreach ($form['#comment_upload_storage'] as $fid => $file) {
		$count++;
	}
	if ($count > 2) {
		form_set_error('Step 2 (Optional): Insert a picture', t('You can only upload a maximum of two(2) files'));
	} else {
		comment_upload_comment_form_validate($form, &$form_state);
	}
	return;
}
gausarts’s picture

Looking for the same feature. Any progress on this? Sorry to ask. Thanks

NathanM’s picture

Subscribing

marinex’s picture

Issue summary: View changes

You can add to comment_upload_comment_form_validate() this code before the last return:

  $count = 0;
  // Count number of files
  foreach ($form['#comment_upload_storage'] as $fid => $file) {
    if ($file['remove'] == 0) {
      $count++;
    }
  }
  if ($count > 2) {
    form_set_error('', t('You can only upload a maximum of two(2) files.'));
  } 

the whole function:

function comment_upload_comment_form_validate($form, &$form_state) {
  if (empty($form_state['values']['op']) && (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] != t('Attach'))) {
    return;
  }
  elseif(!empty($form_state['rebuild'])) {
    return;
  }

  comment_upload_process_files($form, $form_state);
  foreach ($form['#comment_upload_storage'] as $fid => $file) {
    if (is_numeric($fid)) {
      $form['#comment_upload_storage'][$fid]['description'] = $form_state['values']['files'][$fid]['description'];
      $form['#comment_upload_storage'][$fid]['list'] = $form_state['values']['files'][$fid]['list'];
      $form['#comment_upload_storage'][$fid]['remove'] = $form_state['values']['files'][$fid]['remove'];
      $form['#comment_upload_storage'][$fid]['weight'] = $form_state['values']['files'][$fid]['weight'];

      $form_state['storage']['comment_upload_storage'][$fid] = $form['#comment_upload_storage'][$fid];
    }
  }
  if ((isset($form_state['values']['op']) && $form_state['values']['op'] == t('Preview')) || (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] == t('Attach'))) {
    $form_state['rebuild'] = TRUE;
  }
  elseif (!empty($form_state['submitted'])) {
    unset($form_state['storage']);
  }

  $count = 0;
  // Count number of files
  foreach ($form['#comment_upload_storage'] as $fid => $file) {
    if ($file['remove'] == 0) {
      $count++;
    }
  }
  if ($count > 2) {
    form_set_error('', t('You can only upload a maximum of two(2) files.'));
  } 

  return;
}