Drupal 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);
}
}
当我最终以正常发布方式提交表单时,验证错误被报告为
检测到非法选择。
我是否正确使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对于你的最后一个问题,需要设置
$form_state['ahah_submission'] = TRUE
。i think for your last question, use need to set
$form_state['ahah_submission'] = TRUE
.