在本节中,我们将通过例子来展示内置的Drupal表单元素。
Textfield(文本字段)
元素textfield的示例如下:
$form['pet_name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#description' => t('Enter the name of your pet.'),
'#default_value' => $user->pet_name,
'#maxlength' => 32,
'#required' => TRUE,
'#size' => 15,
'#weight' => 5,
'#autocomplete_path' => 'pet/common_pet_names',
);
$form['pet_weight'] = array(
'#title' => t('Weight'),
'#type' => 'textfield',
'#description' => t('Enter the weight of your pet in kilograms.'),
'#field_suffix' => t('kilograms'),
'#default_value' => $user->pet_weight,
'#size' => 4,
'#weight' => 10,
);
表单元素的显示结果如图10-11所示

图10-11元素textfield
#field_prefix 和 #field_suffix属性是特定于文本字段的,它们在文本字段输入框的前面或者后面紧接着放置一个字符串。
#autocomplete属性定义了一个路径,Drupal自动包含进来的JavaScript将使用jQuery向该路径发送HTTP请求。在前面的例子中,它将请求http://example.com/pet/common_pet_names。实际例子可以参看modules/user/user.pages.inc中的user_autocomplete()函数。
文本字段元素的常用属性如下:#attributes, #autocomplete_path (默认为 FALSE), #default_value, #description, #field_prefix, #field_suffix,#maxlength (默认为128), #prefix, #required, #size (默认为60), #suffix, #title,#process(默认为form_expand_ahah),和 #weight。
老葛的Drupal培训班 Think in Drupal
评论
正确顺序
上一页: < 通用的表单元素属性 下一页 (跳过子节点) : < 总结 >