Drupal AHAH,动态表格扩展

时间:2011-04-05 18:59:54

标签: drupal drupal-6 ahah

表格

$form['animal'] = array(
    '#type' => 'select',
    '#title' => t('Animal'),
    '#options' => load_animals(),
    '#ahah' => array(
        'event' => 'change',
        'path'  => 'path/to/ajax/service',
        'method' => 'replace',
        'effect' => 'fade',
        'wrapper' => 'breed-wrapper',
    ),
);
...
$form['breed'] = array(
    '#type'     => 'select',
    '#title'    => t('Breeds'),
    '#options'  => array('Select animal to load breed'),
    '#prefix'   => '<div id="breed-wrapper">',
    '#suffix'   => '</div>',
);

以下是AHAH回调处理

$post = $_POST;
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $post['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form['#redirect'] = FALSE;
$form['#post'] = $post;
$form['#programmed'] = FALSE;
$form_state['post'] = $post;
drupal_process_form($form_id, $form, $form_state);
// New form elements
$breed_form = $form['breed'];
$options = load_breeds((int)$post['animal']);
$breed_form['#options'] = $options;
$form['breed'] = $breed_form;
form_set_cache($form_build_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

unset($breed_form['#prefix'], $breed_form['#suffix']);
// Render the new output.   
$output .= drupal_render($breed_form);

drupal_json(array('status' => TRUE, 'data' => $output));

默认表单提交处理程序

function default_form_submit(&$form, $form_state){
    $clicked_button = $form_state['clicked_button']['#value'];
    $values = $form_state['values'];
    if($clicked_button == $values['submit']){
        unset($values['op'], $values['submit'], $values['form_build_id'],
            $values['form_token'], $values['form_id']);
        ....
        drupal_goto($_REQUEST['q'], $query);
    }
}
  • 当我最终以正常的邮寄方式提交表单时,验证错误报告为An illegal choice has been detected.我是否正确使用form_set_cache()?

  • 在AHAH帖子中,还会调用默认表单提交处理程序。由于此处理程序包含重定向逻辑,因此AHAH请求已折叠。即使我正在进行click_button验证,如何绕过它?

1 个答案:

答案 0 :(得分:0)

我想你的上一个问题是,需要设置$form_state['ahah_submission'] = TRUE