0

I have a problem with "AJAX: add more file fields". The Problem: i create new managed_file field and add AJAX function "add more file fields". When i try to upload a file with first static file field, it works fine. But when use function "add more fields" and try to upload a file with AJAX created field, get error "Notice: Undefined index: ... in file_ajax_upload() (Line 262 in .../modules/file/file.module)."

All Answers for Similar Questions here don't solve this Problem.

The code:

<?php
/**
* implimentation of hook_form_alter()
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {

}

/**
* implimentation of hook_form_FORM_ID_alter()
*/

function mymodule_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
 // Since the form builder is called after every AJAX request, we rebuild
  // the form based on $form_state.
  $num_checkboxes = !empty($form_state['values']['howmany_select']) ? $form_state['values']['howmany_select'] : 1;
  $form['howmany_select'] = array(
    '#title' => t('How many checkboxes do you want?'),
    '#type' => 'select',
    '#options' => array(
      1 => 1,
      2 => 2,
      3 => 3,
      4 => 4,
    ),
    '#default_value' => $num_checkboxes,
    '#ajax' => array(
      'callback' => 'ajax_example_autocheckboxes_callback',
      'wrapper' => 'checkboxes-div',
      // See ajax_example_autotextfields for an example of 'fade'.
      'effect' => 'fade',
      // Don't show any throbber...
      'progress' => array('type' => 'none'),
    ),
  );

  $form['checkboxes_fieldset'] = array(
    '#title' => t("Generated Checkboxes"),
    // The prefix/suffix provide the div that we're replacing, named by
    // #ajax['wrapper'] above.
    '#prefix' => '<div id="checkboxes-div">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we get automatically generated checkboxes'),
  );

  for ($i = 1; $i <= $num_checkboxes; $i++) {
   $form['checkboxes_fieldset']["slideshow_item_$i"] = array(
    '#title'    => t('Image')." $i:",
    '#type'     => 'managed_file',
    '#required' => FALSE,
    '#upload_location' => file_default_scheme() . '://mymodule_images/',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
    '#tree' => TRUE,
    '#description' => t("Upload slideshow image. Allowed extensions: gif, png, jpg, jpeg."),
   );
  }
  return $form;
}



function ajax_example_autocheckboxes_callback($form, $form_state) {
  $form_state['rebuild'] = TRUE;
  return $form['checkboxes_fieldset'];
}

?>

I think the Problem is in $form_state. How i can refresh / rebuild $form_state? In ajax_example_autocheckboxes_callback i set $form_state['rebuild'] = TRUE; but without effect :(

Code Test Video:

http://www.youtube.com/watch?v=PtRiQorCTA4

I looking 3 days for a solution :-/

Helpful articles:

How to add more Fields using Form Api

https://drupal.org/node/752056

https://drupal.org/node/331941

How to wrap managed_file Form API element with custom HTML using #theme_wrappers?

2 Answers 2

1

You need to pass the parameters by reference if you want changes to persist outside the function scope:

function ajax_example_autocheckboxes_callback(&$form, &$form_state) {
1
  • Thanks for the tip, but I have found another solution.
    – QQQ
    Dec 23, 2013 at 21:25
0

OK, i have it!

The right way: "Submit button":

  $form['add_file_field'] = array(
                     '#type' => 'submit',
                     '#value' => t('Add one more'),
                     '#submit' => array('add_more_file_fields'),
                     '#ajax' => array(
                         'callback' => 'ajax_example_autocheckboxes_callback',
                         'wrapper' => 'ajax-generated-fields',
                     ),
                 );

Function, called after submit:

 function add_more_file_fields($form, &$form_state) {
   $form_state['values']['howmany_select']++;
   $form_state['rebuild'] = TRUE;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.