0

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. form image

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

0

Give the textfield wrappers unique ids, e.g. fuel_design_tool-wrapper-1 and fuel_design_tool-wrapper-2, and assign accordingly in the FAPI array.

  • Before your reply i made some changes to my code.i have no problem with fiedset1.In fieldset2 if i select value from dropdown2,the textfield2 is empty but filled with correct value when i click on "add more fieldset" button. please find my edited code above in my question. If you want me to provide unique ids for wrappers can you please be bit clear or more explanation please – Naveena Nov 27 '14 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.