【问题标题】:drupal ajax formdrupal ajax 表单
【发布时间】:2011-12-21 14:44:42
【问题描述】:

我想做的是:我想在带有视图的页面上呈现表单。此视图有一个“注释”列表 (=CT Note)。当您填写表格时,便条被存储,表格被清除,新的便条被添加到列表中。全部无需刷新页面。

我创建了一个模块new_note,并添加了这个功能:

    function new_note_form($nodeid = NULL) {
        ctools_include('ajax');
        //  drupal_add_js(drupal_get_path('module', 'custom_forms') . '/js/note_form.js');
        //dpm($nodeid);
        module_load_include('inc', 'node', 'node.pages');
        $form = node_add('note');
        $form['field_note_reference']['und']['#value'] = '2';
        $form['field_note_reference']['und']['#validated'] = 'TRUE';
        $form['field_note_reference']['#attributes']['class'][] = "hidden";
        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => 'Submit',
            '#executes_submit_callback' => FALSE,
            '#ajax' => array(
                'callback' => 'ajax_note',
                'wrapper' => 'status',
            ),
        );
     $output= drupal_render($form);
        dpm($form);
        print $output;
    }

function ajax_note(&$form, &$form_state) {
    return 'test';
}

我在显示套件块字段中使用此功能,该字段呈现在笔记列表上方。到目前为止一切顺利。

唯一的问题是,当我提交表单时,没有调用ajax,正常提交。

谁能帮帮我

@编辑。

根据 clive 的建议,我更改了代码,并让 ajax 正常工作。

function new_notes_form($nodeid = NULL) {
    global $user;

    $node = (object) array(
                'uid' => $user->uid,
                'name' => (isset($user->name) ? $user->name : ''),
                'type' => 'note',
                'language' => LANGUAGE_NONE,
    );
    $form_state = array();
    $form_state['build_info']['args'] = array($node);
    form_load_include($form_state, 'inc', 'node', 'node.pages');
    $form = drupal_build_form('note_node_form', $form_state);
    $form['field_note_reference']['und']['#value'] = '2';
    $form['field_note_reference']['#attributes']['class'][] = "hidden";
    $form['submit'] = array(
        '#type' => 'button',
        '#value' => 'Submit',
        '#limit_validation_errors' => array(),
        '#ajax' => array(
            'callback' => 'ajax_note_replace',
            'wrapper' => 'status',
        ),
    );
    return $form;
}

function ajax_note_replace(&$form, &$form_state) {
    dpm("test");
    dpm($form);
    $output = '<h1>' . t('Hello World') . '</h1>';
    // $node = node_load('6');
    // $output .= drupal_render(node_view($node, $style = 'teaser', $options = array()));

    ctools_include('ajax');
    $commands = array();
    $commands[] = ajax_command_prepend(".view-content", $output);
    print ajax_render($commands); // this function exits.
    exit;
}

@Clive,你能帮我解决剩下的问题吗?我想在回调中保存节点(如果有效?)。在 ajax 回调中,我的节点 ID 为空,因为它尚未存储?如何验证和保存节点,否则将无效的表单项设置为正常红色。

【问题讨论】:

  • 你能把advanced_form_callback()的代码贴出来吗?
  • 抱歉版本有误,更新了

标签: php ajax forms drupal drupal-7


【解决方案1】:

这可能是因为您回避了 Drupal 用于创建表单的正确方法,因此不会执行 AJAX 预处理。如果您查看 drupal_get_form()(该函数几乎专门用于在 Drupal 中准备表单),您会看到它依次调用 drupal_build_form(),这是您需要做的:

function new_note_form($form, &$form_state, $nodeid = NULL) {
  $form_state['build_info']['args'] = array('note');
  $form = drupal_build_form('node_add', $form_state);

  $form['submit'] = array(
    '#type' => 'button',
    '#value' => 'Submit',
    '#limit_validation_errors' => array(),
    '#ajax' => array(
        'callback' => 'advanced_form_callback',
        'wrapper' => 'status',
    ),
  );

  return $form;
}

echo render(drupal_get_form('new_note_form'));

我已将元素类型从 submit 更改为 button,因为我发现它在您想做一些 ajax 处理时效果更好,删除了 #executes_submit_callback,并添加了 #limit_validation_errors,这将停止表单否则验证(我认为这就是你试图通过将#validated 设置为TRUE 来做的,但我可能错了)。

希望对您有所帮助,它未经测试,但应该为您提供一个好的起点。

【讨论】:

  • 嗯,我得到:未定义的索引:drupal_retrieve_form() 中的 node_add
  • 啊,是的,抱歉,在顶部添加module_load_include('inc', 'node', 'node.pages');,就像您在原始函数中所做的那样。您还应该从您的 AJAX 回调中返回一个表单元素,或者使用 AJAX functions 构建的 ajax 命令
  • 你能帮我验证和保存节点吗?
  • 表单应该通过正常的验证/提交处理程序,节点应该被验证并正常保存。如果没有,您可能需要重新附加一些处理程序或其他东西。您最好为此发布另一个问题,在 cmets 中讨论它会很困难。如果您这样做,我会留意并尝试提供帮助:)
  • 好主意,如果您可以在不包含节点编辑表单的情况下这样做,它将为您省去很多麻烦。然后你只需要在提交处理程序中使用node_save() 函数将节点保存到数据库中就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多