Drupal 7 Drupal 7表单API-AJAX表单错误:检测到非法选择。请联系网站管理员

Drupal 7 Drupal 7表单API-AJAX表单错误:检测到非法选择。请联系网站管理员,drupal-7,drupal-modules,drupal-forms,drupal-ajax,Drupal 7,Drupal Modules,Drupal Forms,Drupal 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'];
}

我曾多次遇到此错误,我发现如下:

这是Drupal的表达方式,嘿,您试图提交此表单时使用了原始表单定义中未包含的、复选框或单选按钮选项!那是不允许的更多信息,请访问:http://proofgroup.com/blog/2008/jul/debugging_mysterious_illegal_choice_has_been_detected_please_contact_site_administratosthash.vDNmqslL.dpuf

代替自定义代码,也许你应该考虑模块


或者一个解决方法:“验证”=>TRUE

实际上,它是“验证”=>TRUE。

当您收到非法选择消息时,您可以转到管理->报告->最近的日志条目并报告那里显示的错误吗?消息在测量方法元素中显示非法选择5。5是我测试时从该字段中选择的值。我在这里回答了您的问题,因为如果您确信ajax填充的值一定有效,或者您不太关心客户端用户调整的值>>“验证”=>TRUE就足够了,那么这是一个重复的问题。。