I am having an annoying problem whereby $form_state['values']['region'] and $form_state['values']['country'] are being cleared when I upload an image via the managed_file field. This is causing a "...tid cannot be null" error upon DB insertion (of course because the values are now null after managed_file upload). If I disable the managed_file field everything works as it should but of course I need the upload!
I am relatively new to Drupal and thus not quite understanding why this is acting like it is. Can anyone help me out as this is driving me nuts!
My form
function myforms_company_add_form($form, &$form_state, $node) {
$form['#prefix'] = '<div id="wrapper">';
$form['#suffix'] = '</div>';
if(!isset($form_state['values']['region']) && !isset($form_state['values']['country'])) {
$selectedRegion = array_shift(taxonomy_get_parents($node->field_company_region['und'][0]['tid']))->tid;
$selectedCountry = $node->field_company_region['und'][0]['tid'];
}
else {
$selectedRegion = $form_state['values']['region'];
$selectedCountry = $form_state['values']['country'];
}
$form['region'] = array(
'#title' => t('Region'),
'#type' => 'select',
'#required' => TRUE,
'#options' => _get_region_list('region'),
'#prefix' => '<div id="contact-selector"><div id="region-wrapper">',
'#suffix' => '</div>',
'#default_value' => $selectedRegion,
'#ajax' => array(
'callback' => 'region_ajax_callback',
'wrapper' => 'wrapper',
),
);
$form['country']['wrapper'] = array(
'#type' => 'markup',
'#prefix' => '<div id="country-wrapper">',
'#suffix' => '</div></div>',
);
if (isset($selectedRegion)) {
$form['country'] = array(
'#title' => t('Country'),
'#type' => 'select',
'#required' => TRUE,
'#options' => _get_country_list($selectedRegion),
'#default_value' => $selectedCountry,
'#ajax' => array(
'callback' => 'region_ajax_callback',
'wrapper' => 'wrapper',
),
);
}
$form['company_logo'] = array(
'#title' => t('Logo'),
'#type' => 'managed_file',
'#upload_location' => 'public://my-files/',
'#required' => FALSE,
'#default_value' => $node->field_company_logo['und'][0]['fid'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#prefix' => '<hr>',
);
return $form;
}
Then my callback
function region_ajax_callback($form, &$form_state) {
return $form;
}