Nea*_*alv 1 php forms ajax drupal drupal-7
我想要做的是:我想在一个带有视图的页面上呈现一个表单.该视图有一个'notes'列表(= 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';
}
Run Code Online (Sandbox Code Playgroud)
我在显示套件块字段中使用此函数,该字段在注释列表上方呈现.到现在为止还挺好.
唯一的问题是,当我提交表单时,不会调用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;
}
Run Code Online (Sandbox Code Playgroud)
@Clive,你可以帮我解决剩下的问题吗?我想在回调时保存节点(如果有效?).在ajax回调中,我的node-id为null,因为它尚未存储?如何验证和保存节点,否则将无效表单项设置为正常红色.
这很可能是因为你正在回避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'));
Run Code Online (Sandbox Code Playgroud)
我已经将元素从类型更改为submit,button因为我发现当你想要进行一些ajax处理,删除#executes_submit_callback和添加时#limit_validation_errors,它会更好地工作,这会阻止表单进行其他验证(我认为这是你试图通过设置#validated为TRUE但我可能错了).
希望有所帮助,这是未经测试但应该给你一个好的起点.