TransWikia.com

Ajax callback does not replace the form but add a new field

Drupal Answers Asked by billyJoe on January 6, 2022

I use his code to populate a select after giving à zipcode.

$form['responsible']['zipcode'] = array(
      '#type'               => 'textfield',
      '#title'              => t('Zipcode'),
      '#default_value'      => empty($account->Dossier->responsable->adresse->codePostal) ? Null : $account->Dossier->responsable->adresse->codePostal,
      '#required'           => True,
      '#maxlength'          => 5,
      '#ajax' => array(
        'callback' => 'ajax_get_cities_callback',
        'wrapper' => 'edit-city',
        'method' => 'replace',
      ),
    );

    $form['responsible']['city'] = array(
      '#type'               => 'select',
      '#title'              => t('City'),
      '#options'            => array(),
      '#required'           => True,
    );

My callback

function ajax_get_cities_callback($form, $form_state){
  $client = new SoapClient('myWebservice',array("trace" => true,  "exceptions" => true,));
  $requestParams = array(
    'parameters' => array (
      'numSite' => 1058,
      'typeListes' => array(
        'RechercheTypeListe' => array(
          'typeListe' => 'LST_VILLE',
          'param1' => $form_state['values']['zipcode']
          )
        )
    )
  );
  $dataresponse->requestParams = $requestParams;
  $response = "";
  try {
      $response = $client->__Call('GetListes',$requestParams);
  }
  catch (Exception $e) {
    var_dump($e);
    return False;
  }

  $form['responsible']['city']['#options'] = array(
    0 => "aaaa",
    1 => "test2",
    2 => "test3"
  );

  return $form['responsible']['city'];

}

When I give a zipcode, a new select is created behind the first one.( picture here: https://www.uploady.com/#!/download/zjKvglzRTSe/ZIMYkjSAAaLrUFUq)
I tried to return only $form[‘responsible’][‘city’][‘#options’] but it does not fixe.

Any idea ?

Thank you

One Answer

You have to setup a wrapper on the field you want to update :

$form['responsible']['zipcode'] = array(
      '#type'               => 'textfield',
      '#title'              => t('Zipcode'),
      '#default_value'      => empty($account->Dossier->responsable->adresse->codePostal) 
                                    ? Null 
                                    : $account->Dossier->responsable->adresse->codePostal,
      '#required'           => True,
      '#maxlength'          => 5,
      '#ajax' => array(
        'callback' => 'ajax_get_cities_callback',
        'wrapper' => 'div_city_wrapper', // Changed here
        'method' => 'replace',
      ),
    );

    $form['responsible']['city'] = array(
      '#type'               => 'select',
      '#title'              => t('City'),
      '#options'            => array(),
      '#required'           => True,
      '#prefix'           => '<div id="div_city_wrapper">', // Added this
      '#suffix'           => '</div>', // Added this
    );

The callback function seems to be fine as it returns the wrapped form element ($form['responsible']['city'])

Answered by yilmi on January 6, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP