Missing my custom ajax field values by image field in node form alter

Created on 17 March 2015, over 9 years ago
Updated 19 July 2024, 4 months ago

Hi,

I create new content type, and add 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:

Audi
- A4
- A6
- Q5
...
BMW
- Z4
- 320
....

In my content type, first reference select list depth setting is 1 so show only brands,
In Second reference select list (Models) content_taxonomy_opt_groups => 1,

I have create simple module, for altering node form and creating custom ajax on Brands and Models select list,

When select a brand, models field show only models of selected brand...

My ajax was worked

Problem is: when upload an image (Ajax upload) and click Publish this error 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 don,t 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.

How can i solve this problem and keep $form_state['values']['car_brand'] after image upload fired,

I have tested on other fields $form_state['values'], and nothing has returned...

It's a couple of days i involved with this problem,
Please Help
Thanks

πŸ’¬ Support request
Status

Closed: outdated

Version

7.0 ⚰️

Component
AjaxΒ  β†’

Last updated about 15 hours ago

Created by

πŸ‡΅πŸ‡±Poland mahyarss

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024