Drupal AHAH,动态表单扩展

发布于 2024-10-30 10:56:45 字数 2203 浏览 0 评论 0原文

表单

$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);
    }
}
  • 当我最终以正常发布方式提交表单时,验证错误被报告为 检测到非法选择。 我是否正确使用form_set_cache()?

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

Form

$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>',
);

And following is the AHAH callback processing

$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));

Default form submit handler

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);
    }
}
  • When I finally submit the form in normal post way, a validation error is reported as An illegal choice has been detected. Am I properly using form_set_cache()?

  • On AHAH post, the default form submission handler is also invoked. As this handler contains a redirection logic so AHAH request is collapsed. How to bypass it even-though I am doing click_button validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-11-06 10:56:45

我认为对于你的最后一个问题,需要设置$form_state['ahah_submission'] = TRUE

i think for your last question, use need to set $form_state['ahah_submission'] = TRUE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文