Drupal 7 Forms API - AJAX Forms错误:检测到非法选择。请联系网站管理员 [英] Drupal 7 Forms API - AJAX Forms error: An illegal choice has been detected. Please contact the site administrator

查看:369
本文介绍了Drupal 7 Forms API - AJAX Forms错误:检测到非法选择。请联系网站管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单设置,用户从一个下拉列表中选择一个项目。选择该项目后,另一个下拉列表将被填充。然后根据从第二个下拉列表中选择的值,可以显示也可能不显示字段集。如果显示字段集,则会显示一个字段和一个按钮。通过单击按钮,您可以添加相同字段的另一个副本。一旦有多个删除一个按钮也显示。我从这里获得了代码的基础:
http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7

I have a form set up where a users selects an item from a drop down. Once that item is selected another drop down is populated. Then depending on the value that is selected from the second drop down a fieldset may or may not be shown. If the field set is shown there is a field and a button. By clicking the button you add another copy of the same field. Once there is more than one a remove one button shows up as well. I got the basis for the code from here: http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7

问题是当我点击添加另一个调查问题,第一次工作正常并添加一个字段。当我点击它第二次或当我点击删除现在有两个,我得到这个错误:一个非法的选择被检测到。请联系网站管理员。

The problem is when I click 'Add Another Survey Question' the first time it works fine and add a field. When I click it a second time or when I click remove now that there are two, I get this error: 'An illegal choice has been detected. Please contact the site administrator.'

我做错了什么?

这是我的代码:

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'];
}


推荐答案

其实这是'#validate d '=> TRUE。

Actually, it's '#validated' => TRUE.

这篇关于Drupal 7 Forms API - AJAX Forms错误:检测到非法选择。请联系网站管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆