I have created new content type and added two taxonomy reference field (select list) both referenced to same vocabulary "car" and one Image field with cardinality = 6,
In car vocabulary listed brands and models like:
Brand 1
- model 1
- model 2
- model 3
...
Brand 2
- model 1
- model 2
....
In my content type, first reference select list depth setting is 1, so displays only brands, In Second reference select list (Models) content_taxonomy_opt_groups => 1,
I have created simple module, for altering on node form and creating custom ajax on Brands and Models select list,
When select a brand, models field displays only models of selected brand...
My ajax has worked
Problem is: when upload an image (Ajax upload) and click Publish this error is shown:
An illegal choice has been detected. Please contact the site administrator.
/**
* Implements hook_form_FORM_ID_alter().
*/
function car_info_form_car_info_node_form_alter(&$form, &$form_state, $form_id) {
$brand_options = $form['car_brand'][LANGUAGE_NONE]['#options'];
$selected_brand = isset($form_state['values']['car_brand']) ? $form_state['values']['car_brand'][LANGUAGE_NONE][0]['tid'] : key($brand_options);
$form['car_brand'][LANGUAGE_NONE]['#default_value'] = $selected_brand;
$form['car_brand'][LANGUAGE_NONE]['#ajax'] = array(
'callback' => 'car_info_dependent_model_callback',
'wrapper' => 'dropdown-model-replace',
'effect' => 'fade',
);
$form['car_model'][LANGUAGE_NONE]['#prefix'] = '<div id="dropdown-model-replace">';
$form['car_model'][LANGUAGE_NONE]['#suffix'] = '</div>';
if ($selected_brand > 0) {
$model_options = _car_info_get_model_dropdown_options($selected_brand);
$selected_model = isset($form_state['values']['car_model']) ? $form_state['values']['car_model'][LANGUAGE_NONE][0]['tid'] : key($model_options);
$form['car_model'][LANGUAGE_NONE]['#options'] = $model_options;
$form['car_model'][LANGUAGE_NONE]['#default_value'] = $selected_model;
}
else {
// Remove default options (All models of brands) if brand not selected
$form['car_model'][LANGUAGE_NONE]['#options'] = _car_info_get_model_dropdown_options('');
}
// $form['#validate'][] = 'car_info_form_car_info_node_form_validate';
}
function car_info_dependent_model_callback($form, $form_state) {
return $form['car_model'];
}
function _car_info_get_model_dropdown_options($brand_id) {
if (isset($brand_id)) {
$car_models_tree = taxonomy_get_children($brand_id);
$models[''] = t('Select model');
foreach ($car_models_tree as $model) {
$models[$model->tid] = $model->name;
}
}
else {
$models[''] = t('First select brand');
}
return $models;
}
function car_info_form_car_info_node_form_validate($form, &$form_state) {
$car_brand = $form_state['input']['car_brand'][LANGUAGE_NONE];
$form_state['values']['car_brand'][LANGUAGE_NONE][0]['tid'] = $car_brand;
}
If I dont add image or add image without click on upload button (upload handled when clicked on publish directly) in both way my form is submitted correctly
But if click upload button in image field (Upload handled by ajax), this issue is happened and $form_state['values']['car_brand'] is empty when using ajax image upload.