Failed to save dependent select block values โ€‹โ€‹in Drupal 7

I have two dependent drop-down lists for the country and one for the state. I use this concept several times in a multi-stage web form. When the form loads, the list of countries is in order. When choosing a specific country, the list of conditions is also in order. Now the question arises: If there is some required field in the same part of the web form and the user does not fill it out, the form is updated and loses the entire list of states. In addition, when you move to the next step (multi-stage web form) and return to the same page, the value is lost. However, throughout the process, the value of the country selection list is retained. Selection options generated with ajax are not saved. Thanks in advance. Below is the code.

$form['submitted']['employment_history']['employer_1']['address_of_employer']['country']['#ajax'] = array( 'callback' => 'my_custom_ajax_callback_for_employer_one', 'wrapper' => 'edit-submitted-employment-history-employer-1-address-of-employer-state', 'method' => 'replace', ); /* * Implements Ajax callback for populating list of provinces (Employer One). */ function my_custom_ajax_callback_for_employer_one($from, $form_state) { $selected_country = $form_state['values']['submitted']['employment_history']['employer_1']['address_of_employer']['country']; $states = location_get_provinces($selected_country); $form['submitted']['employment_history']['employer_1']['address_of_employer']['state']= array( '#type' => 'select', '#options' => $states, '#attributes' => array('id' => 'edit-submitted-employment-history-employer-1-address-of-employer-state'), ); $form['rebuild'] = TRUE; return $form['submitted']['employment_history']['employer_1']['address_of_employer']['state']; } 
+4
source share
1 answer

My initial thought is that you should pass the form and the state of the form by reference ($ from, $ form_state) should be (& $ form, & $ form_state) in your ajax callback (you also have a typo).

0
source

Source: https://habr.com/ru/post/1496601/


All Articles