0

I'm trying to make a dependent AJAX dropdown form. I create a dynamic form and I render it. But when I do the callback function (chili_callback) and I dpm my form, the newly added form items are not in the form array anymore.. So I can't replace the following dropdown..Any suggestions?

function chili_dependent_dropdown_form($form, &$form_state)
{

$term = taxonomy_term_load(arg(2));
$options = get_settings(strtolower($term->name));
$options_first = _chili_get_first_dropdown_options($options['0']);
$selected = isset($form_state['values'][$options['0']]) ? $form_state['values'][$options['0']] : key($options_first);
$form = array(
    '#prefix' => '<div id="categories">',
    '#suffix' => '</div>'
);

foreach($options as $key => $value) {

    $first = array();
    if ($key == 0) {
        $first = _chili_get_first_dropdown_options($options['0']);
    }
    else {
        $first = array(
            '0' => DEFAULT_VALUE
        );
    }

    $form[$value] = array(
        '#type' => 'select',
        '#title' => 'Select ' . str_replace("_", " ", $value) ,
        '#prefix' => '<div id="' . $value . '">',
        '#suffix' => '</div>',
        '#validated' => TRUE,
        '#options' => $first,
        '#defalt_value' => $selected,
        '#ajax' => array(
            'callback' => 'chili_callback',
            'effect' => 'fade',
        )
    );
}

return $form;
}
function chili_callback($form, $form_state)
{


$form_state['rebuild'] = TRUE;
 dpm($form);
global $base_url;
$action = $form_state['complete form']['#action'];
$ex = explode("/", $action);
$options = get_settings($ex[2]);

$previous = $form_state['triggering_element']["#name"];
$previous_value = $form_state['triggering_element']["#value"];
$product = "Unicover";

$link = "";
foreach($options as $option) {
    if ($form_state['values'][$option] > 0) {
        $term = taxonomy_term_load($form_state['values'][$option]);
        $link.= "_" . $term->name;
    }
}

$ext = IMAGE_EXTENSION;
$image_url = $product . "" . $link . "" . $ext;
$key = array_search($previous, $options);
$next = $options[$key + 1];

// unset($form[$options[0]]['#options']['0']);

$get_options = _chili_get_options($form_state, $previous);

$form[$next]['#options'] = $get_options['options'];
$form[$next]['#attributes'] = array(
    'class' => array(
        'active'
    )
);

$commands = array();
$base = $base_url . "/sites/print/files";
$file = $base . "/" . $image_url;
if (url_exist($file)) {
    $image = "<div id='thumb'><img src='" . $file . "'/></div>";
    $commands[] = ajax_command_replace("#thumb", $image);
}

$base = $base_url . "/product/template/personalise";
if ($next == $options[count($options) ]) {
    $link = $base . "/" . $get_options['result'];
    $a = '<a class="enabled" href="' . $link . '" id="personalise">Personalise</a>';
    $commands[] = ajax_command_replace("#personalise", $a);
}

$commands[] = ajax_command_alert($form[$next]);
dpm($form[$next]);
return array(
    '#type' => 'ajax',
    '#commands' => $commands,
);



}
  • What is in chili_callback ? Also, remember that any persistent changes to form must be made in form creation function, using data from $form_state. Only use callback to provide form part to render and to make one-time changes. – Mołot Sep 5 '14 at 12:49
  • I've added chili_callback. – Pieter Moeyersons Sep 5 '14 at 13:06
  • If the problem is that changes to form made in callback are not persistent then yes, that's how it was designed to work. – Mołot Sep 5 '14 at 13:16
  • I had it working like this.. – Pieter Moeyersons Sep 5 '14 at 13:41
  • Should you not be looking inside $form_state in order to check whether form elements have been rendered in your form and have been passed to the callback function? dpm($form_state['values']); Should allow you to see whether a form element has been rendered and/or populated with submitted data. Or am I completely misinterpreting what you're asking? – Adam Thomason Sep 5 '14 at 13:58

Your Answer

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

Browse other questions tagged or ask your own question.