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?