run*_*day 5 forms drupal drupal-6 drupal-modules
这是专业drupal书的一个例子.
function annotate_admin_settings() {
$options = node_get_types('names');
$form['annotate_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Users may annotate these content types'),
'#options' => $options,
'#default_value' => variable_get('annotate_node_types', array('page')),
'#description' => t('A text field will be available on these content types to
make user-specific notes.'),
);
return system_settings_form($form);
}
Run Code Online (Sandbox Code Playgroud)
但是形成drupal文档,表单风格的构建器就是这样.
function mymodule_myform($form_state) {
}
Run Code Online (Sandbox Code Playgroud)
函数中有一个参数($ form_state),当我应该使用这个参数时.谢谢你.
$formthosewhatnots 在讨论 和之间的区别时是完全正确的$form_state。 $form定义表单,$form_state携带有关已处理表单的信息。
那么为什么表单构建函数将$form_state其作为第一个参数(以及任意数量的可选的、用户指定的参数)?提供构建表单时可能需要的上下文和状态信息。
表单构建函数应始终返回表单定义,但它们可能会根据最后单击的按钮、用户提交的值或其他信息等信息做出有关表单定义的决策。正如 ceejayoz 在评论中提到的,$form_state构建器函数中的常见用途是处理多步骤表单,其中表单的状态(“步骤 1 + 2 已完成,步骤 3 仍待进行)对表单有影响显示给用户的。
在许多常见用例中,$form_state完全被忽略,并且可选参数用于提供上下文信息(有关许多示例之一,请参阅comment_form API 文档)。