继续Drupal分享精神 QQ群:107748121 站长QQ:2275288328 skype: hellodrupal【drupal交流+drupal 建站+theme制作】
登录 注册

Drupal Form API hook 使用

用drupal,也有一段时间,经常会处理一些form,感觉drupal上开发form更加简单快速。 比如想在一个页面添加一个表单,让游客填写,当然这也可以使用现成的第三方模块来完成,也很方便。 如果对第三方模块开发,额外添加一些功能,可能要使用到form api。 Form api 一些资料 http://drupal.org/node/37775 官方的一些form api 资料和实例 这里简单来做一个在profile 模块里面添加额外一个文本框,然后进行验证

<?php

/**
*hook_form_FORM_ID_alter
*/

function hook_form_user_profile_form_alter(&$form,&$form_state){
      
$form[] = test_form();//添加新的form 表单
   
$form['#validate'][]=  'hook_user_profile_validate';//对表单进行验证处理
}

function
hook_user_profile_validate($form,&$form_sate){
    if(
$form_sate['values']['test_title'] == ''){
       
form_set_error('test_title',t('请注意:Test tilte 文本框不能为空'));//如果testtitle表单为空然后给一个错误提示处理
   
}
}


function
test_form(){//定义from 表单数组
   
$form = array();//表单
   
$form['test_title'] = array(
       
'#type' =>'textfield',//文本框
       
'#title' => t('Test title'),//标题
       
'#size' => 30,//长度
       
'#description' => t('HI,Please add the code into the textarea')//内容提示
       
);
    return
$form;
}
?>

效果如图

http://hellodrupal.info/sites/default/files/drupal_form_api.png

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.