In my custom form i am dynamically generating multiple fieldsets.Each field set has select option(dropdownlist) and textfield generated dynamically. The value for textfield is dynamically generated based on user selected value from select option.
My problem is that if i select species from fieldset2 then density texfield1 in fieldset1 is overwritten and density textfield2 in fieldset2 is empty which is wrong. Each time the ajax callback return value is overwriting textfield1 in fieldset1(Field#1)
function fuel_design_tool($form, &$form_state) { $form = array(); $form['#tree'] = TRUE; if (empty($form_state[FIELDS_NUM])) { $form_state[FIELDS_NUM] = 1; } for ($i = 1; $i <= $form_state[FIELDS_NUM]; $i++) { $form['field'][$i] = array( '#type' => 'fieldset', '#title' => t('Field #@num', array('@num' => $i)), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['field'][$i][FORM_FIELD_SPECIES] = array( '#type' => 'select', '#required' => TRUE, '#title' => 'Choose Species', '#options' => _load_species1(), '#default_value' => 0, '#ajax' => array( 'callback' => 'fuel_design_tool_callback', 'wrapper' => 'fuel_design_tool-wrapper', 'event' => 'change', ) ); $profile = isset($form_state['values']['field'][$i]['FORM_FIELD_SPECIES']) ? ($form_state['values']['field'][$i]['FORM_FIELD_SPECIES']) : NULL; $form['field'][$i][FORM_FIELD_DENSITY] = array( '#type' => 'textfield', '#prefix' => '<div id="fuel_design_tool-wrapper">', '#suffix' => '</div>', '#title' => t('Density'), '#value' =>$profile, ); $form['add_field'] = array( '#type' => 'submit', '#value' => t('Add More fieldet'), '#submit' => array('fuel_design_tool_add_name') ), $form['submit_button'] = array( '#type' => 'submit', '#value' => t('Submit'), "#submit" => array("fuel_design_tool_submit"), ); return $form; } function fuel_design_tool_add_name($form, &$form_state) { $form_state[FIELDS_NUM]++; $form_state['rebuild'] = TRUE; } function fuel_design_tool_callback($form, &$form_state) { for ($i = 1; $i <= $form_state[FIELDS_NUM]; $i++) { $form['values']['field'][$i]['FORM_FIELD_DENSITY'] = $form_state['values']['field'][$i]['FORM_FIELD_SPECIES']; return array ( $form['field'][$i]['FORM_FIELD_DENSITY'], ); } } }
My callback function returns only one value and that by default filled to fiedset#1 textfield.I guess i need to modify my callback function.
I am looking forward for your kind help.
If my question is not clear please let me know. I am finding bit difficult to explain this problem
Sponsored by SupremePR