2012-10-11 42 views
2

我有一個表單設置用戶從下拉菜單中選擇一個項目。一旦選擇了該項目,就會填充另一個下拉列表。然後根據從第二個下拉列表中選擇的值,字段集可能顯示也可能不顯示。如果顯示字段集有一個字段和一個按鈕。通過點擊按鈕,您可以添加同一個字段的另一個副本。一旦有多個按鈕,就會顯示一個按鈕。我得到了從這裏的代碼的基礎: http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7Drupal 7 Forms API - AJAX表單錯誤:檢測到非法選項。請聯繫網站管理員

問題是,當我點擊'添加另一個調查問題'第一次工作正常,並添加一個字段。當我第二次點擊它時,或者當我點擊刪除,現在有兩個,我得到這個錯誤:'一個非法的選擇已被檢測到。請聯繫網站管理員。'

我在做什麼錯?

這裏是我的代碼:

function touchpoints_addmetric_form($form, &$form_state, $tp_id) { 
    $selectedType = isset($form_state['values']['type']) ? $form_state['values']['type'] : FALSE; 
    $types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1); 
    $form['#tree'] = TRUE; 
    $form['type'] = array(
     '#type' => 'select', 
     '#title' => t('Metric Type'), 
     '#options' => $types, 
     '#required'=>TRUE, 
     '#ajax' => array(
      'event' => 'change', 
      'wrapper' => 'method-wrapper', 
      'callback' => 'touchpoints_method_callback' 
     ) 
    ); 
    $form['measurementmethod'] = array(
     '#type' => 'select', 
     '#title' => t('Measurement Method'), 
     '#required'=>TRUE, 
     '#prefix' => '<div id="method-wrapper">', 
     '#suffix' => '</div>', 
     '#options' => _get_methods($selectedType) 
    ); 
    $form['survey'] = array(' 
     #type' => 'fieldset', 
     '#collapsible' => FALSE, 
     '#states' => array(
      'visible' => array(
       array(
        array(':input[name="measurementmethod"]' => array('value' => '5')), 
        'xor', 
        array(':input[name="measurementmethod"]' => array('value' => '6')), 
        'xor', 
        array(':input[name="measurementmethod"]' => array('value' => '7')) 
       ) 
      ) 
     ) 
    ); 
    $form['survey']['contents'] = array(
     '#type' => 'fieldset', 
     '#collapsible' => FALSE, 
     '#prefix' => '<div id="survey-div">', 
     '#suffix' => '</div>', 
    ); 
    if (empty($form_state['num_surveys'])) { 
     $form_state['num_surveys'] = 1; 
    } 
    for ($i = 1; $i <= $form_state['num_surveys']; $i++) { 
     $form['survey']['contents']['survey_question'][$i] = array(
      '#type' => 'textfield', 
      '#title' => t('Survey Question ' . $i), 
      '#size' => 70, '#maxlength' => 100, 
     ); 
    } 
    $form['survey']['contents']['addsurvey'] = array(
     '#type' => 'submit', 
     '#value' => t('Add Another Survey Question'), 
     '#submit' => array('touchpoints_metrics_survey_add_one'), 
     '#limit_validation_errors' => array(), 
     '#ajax' => array(
      'callback' => 'touchpoints_metrics_survey_callback', 
      'wrapper' => 'survey-div', 
     ), 
    ); 
    if ($form_state['num_surveys'] > 1) { 
     $form['survey']['contents']['removesurvey'] = array(
      '#type' => 'submit', 
      '#value' => t('Remove A Survey Question'), 
      '#submit' => array('touchpoints_metrics_survey_remove_one'), 
      '#limit_validation_errors' => array(), 
      '#ajax' => array(
       'callback' => 'touchpoints_metrics_survey_callback', 
       'wrapper' => 'survey-div', 
      ), 
     ); 
    } 
    return $form; 
} 

function _get_methods($selected) { 
    if ($selected) { 
     $methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2); 
    } else { 
     $methods = array(); 
    } 
    return $methods; 
} 

function touchpoints_method_callback($form, &$form_state) { 
    return $form['measurementmethod']; 
} 

function touchpoints_metrics_survey_add_one($form, &$form_state) { 
    $form_state['num_surveys']++; 
    $form_state['rebuild'] = TRUE; 
} 

function touchpoints_metrics_survey_remove_one($form, &$form_state) { 
    if ($form_state['num_surveys'] > 1) { 
     $form_state['num_surveys']--; 
    } 
    $form_state['rebuild'] = TRUE; 
} 

function touchpoints_metrics_survey_callback($form, &$form_state) { 
    return $form['survey']['contents']; 
} 
+0

你可以去管理 - >報告 - >最近的日誌條目,並報告錯誤,當你收到非法的選擇信息? –

+0

消息顯示「測量方法元素中的非法選項5」。 5是我在測試時從該領域中選擇的值。 – LoneWolfPR

+0

我在這裏回答了你的問題,因爲這是一個重複的問題https://drupal.stackexchange.com/questions/15989/resolve-the-error-an-illegal-choice-has-been-detected/125233#125233 –

回答

1

我也遇到了這個錯誤幾次了,這是我發現:

This is Drupal's way of saying "Hey, you tried to submit this form with a , checkbox or radio button option that wasn't included in the original form definition! That's not allowed." - See more at: http://proofgroup.com/blog/2008/jul/debugging_mysterious_illegal_choice_has_been_detected_please_contact_site_administrato#sthash.vDNmqslL.dpuf

,而不是自定義代碼,也許你應該考慮模塊Conditional Fields

或者一個工作:'#validate' => TRUEsource

+0

如果你確定ajax填充的值肯定是有效的,或者你不太關心用戶在客戶端調用的值。>>'#validate'=> TRUE應該足夠了。 –

1

其實它是'#validate d'=> TRUE。

相關問題