我有一个 Drupal 表单,其中有人输入信息,我需要在提交之前进行数据库查询以检查它是否有效。我想有一个按钮,用户可以单击以检查有效性(或者在用户离开该字段后自动完成),然后显示有关他的选择的一些信息。
我知道我可以在提交表单时使用 hook_form_submit 来查看表单,如果有任何错误则停止该过程,但我希望用户能够在提交表单之前确认他们选择了正确的内容。
问问题
964 次
2 回答
2
我没有亲自尝试过这个模块,但它可能是你正在寻找的:
http://drupal.org/project/ajax
如果您只是在寻找一种实时查找的方法(例如输入图书条形码并获取标题),您还可以使用 Drupal 的自动完成功能,但它需要您编写自己的自动完成功能来处理数据库查找。
于 2011-08-02T15:53:36.077 回答
0
看一下:带有 validate handler 的基本表单。你真的只需要添加一个类似于mymodule_myform_validate($form, &$form_state) { ... }
. 从链接页面:
“这增加了一个新的表单字段和一种使用验证函数验证它的方法,也称为验证处理程序。”
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
// New form field added to permit entry of year of birth.
// The data entered into this field will be validated with
// the default validation function.
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
// This adds a handler/function to validate the data entered into the
// "year of birth" field to make sure it's between the values of 1900
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http://drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form
// followed by '_validate'. This is the default validation function.
function my_module_my_form_validate($form, &$form_state) {
$year_of_birth = $form_state['values']['year_of_birth'];
if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
}
}
?>
于 2011-07-21T17:22:32.557 回答