0

In my form i get the message: "An illegal choice has been detected. Please contact the site administrator." after i didn't select a radio box in the 2nd form. How can i avoid this? NULL doesn´t work and key($type_2) works only for the first call. If I changed the selection in the first form I must select a radio box in the 2nd form. If I don't do this, the message comes and I can`t change the selection of the 1st form ... hope I could it little bit explain.

  $default = isset($input) ? $input : NULL;
  $form['step_2']['options']['type'] = array (
    '#title' => t('Types 1'),
    '#type' => 'radios',
    '#options' => $type, //get the array from a Taxonomy
    '#default_value' => $default,
    '#required' => TRUE,
    '#attributes' => array(
      'class' => array('clearfix'),
      'onkeypress' => array(
        'if(event.keyCode==13){this.form.submit();}'
      ),
    ),
    '#ajax' =>  array(
    'callback' => '_type_ajax_callback',                    
    ),
  );

  $default_2 = isset($input_2) ? $input_2 : key($type_2);
  $form['step_2']['options']['type_2'] = array (
    '#title' => t('Types 2'),
    '#type' => 'radios',
    '#options' => $type_2, //get the array of the Taxonomy which is referenced in the 1st Taxonomy
    '#default_value' => $default_2,
    '#attributes' => array(
      'class' => array('clearfix')
    ),
    '#ajax' => array(
      'event' => 'change',
      'wrapper' => 'calc-wrapper',
      'callback' => '_type_2_ajax_callback',
      'method' => 'replace',
      'keypress' => TRUE,
    ),
  );

  function _type_ajax_callback(&$form, $form_state) {
    return array(
      '#type' => 'ajax',
      '#commands' => array(
        ajax_command_replace("#floor-options", render($form['step_2']['options'])), 
      )
    );
  }

Maybe i need to recreate the $type_2 array, but I didn`k know how i could do this. Need I an extra value recall in the ajax callback?

0

I found a little solution by clearing the default value after output the form. unset($form['step_2']['options']['type_2']['#default_value']);

But if the user goes back in the multistep form and comes to this point the default value isn`t set. It will not be recrate like the #options in the form array.

1
  • Better solution is '#validated' => TRUE, in the 2nd Form field! – Bruno Jun 13 '16 at 11:33

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.