欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal form 表单验证 form validate

shiping1 的头像

http://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_error/6

用户所属入的信息。请参看$form_state()的关键字列表drupal_build_form()。

添加下面的方法到您的current_posts.module模块文件中:

<?php
/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
*/
function current_posts_form_validate($form, &$form_state){
  $max_num = $form_state['values']['current_posts_max'];
  if (!ctype_digit($max_num)){
    form_set_error('current_posts_max', t('You must enter a number for the maximum number of posts to display.'));
  }
}
?>
请记住不要包含PHP的开始和结束符号

测试数据
首先,我们从$form_state队列中获取模块设置表格的数据并把他们存在一个变量中。然后我们便可以查看这是否是一个数字。如果不是,我们就用form_set_error来返回一个错误信息。这个方法的第一个参数表明您的表格的field名,是哪一个field出现了错误。第二个参数是您想向用户显示的信息内容。这个信息是用红色的框包围起来的。

如果用户所输入的信息通过了第一个检查,那么我们要检查它是不是一个正数。如果不是,我们还是用form_set_error来显示错误信息。注意错误信息的string是要用t()方法来翻译一下的。
 

 

 

 

 

 

 

 

 

 

 

 

 

 

//表单函数_validate 验证类似于钩子函数的回调函数
function my_form_validate(&$form, &$form_state)
{
    /**
   * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
   */
    // form_set_error('',$form_values['name']);
  if($form_state['values']['name'] == '')
  {
    form_set_error('',t('You must input your name'));
  }
}
//表单函数_submit 提交函数
function my_form_submit(&$form, &$form_state)
{
     /**
       * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
       */
     drupal_set_message(t('Your name is ').$form_state['values']['name'].'    '.
     t('Your age is ').$form_state['values']['age']);
}
 

 

 

 

//drupal 6
/ * 表单验证
 * @param (array) &$form
 *  表单字段
 * @param (array) &$form_state
 *  表单值,用做判断表单的各种参数,包括用户提交的所有数据
 */
function myform_page_form_validate(&$form, &$form_state) {
 
  /**
   * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
   * 根据需求,验证昵称是否少于 2 个字符,中英文均算一个字符
   * 可使用封装好的计算字符串长度函数:drupal_strlen
   */
  if (drupal_strlen($form_state['values']['name']) < 2) {
    /**
     * 如果少于 2 个字符,调用  form_set_error() 写入错误信息,系统将显示错误信息,同时阻止表单提交
     * form_set_error() 的格式,第一个参数:字段名称,第二个参数:用户将看到的错误信息
     */
    form_set_error('name', '昵称不能少于 2 个字符');
  } else if (drupal_strlen($form_state['values']['title']) > 255) {
    form_set_error('title', '标题长度不能大于 255 个字符');
  }
 
  // 如验证通过,将进入表单提交环节
}
 

 

 

 

 

 

4) 验证类似于钩子函数 表单函数名_validate
//两个参数 必写
//$form_id表单id
//$form_values表单的相当于post,
但是作用比post大,它本质上是从db中取的
//此函数其实是可选的,可以不验证
//drupal 5
function test_form_validate($form_id,$form_values)
{
 form_set_error('',t(You must input your name'));
}

 

 

 

http://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_error/6

用户所属入的信息。请参看$form_state()的关键字列表drupal_build_form()。

添加下面的方法到您的current_posts.module模块文件中:

<?php
/**
* Implements validation from the Form API.
*
* @param $form
*   A structured array containing the elements and properties of the form.
* @param $form_state
*   An array that stores information about the form's current state
*   during processing.
*/
function current_posts_form_validate($form, &$form_state){
  $max_num = $form_state['values']['current_posts_max'];
  if (!ctype_digit($max_num)){
    form_set_error('current_posts_max', t('You must enter a number for the maximum number of posts to display.'));
  }
}
?>
请记住不要包含PHP的开始和结束符号

测试数据
首先,我们从$form_state队列中获取模块设置表格的数据并把他们存在一个变量中。然后我们便可以查看这是否是一个数字。如果不是,我们就用form_set_error来返回一个错误信息。这个方法的第一个参数表明您的表格的field名,是哪一个field出现了错误。第二个参数是您想向用户显示的信息内容。这个信息是用红色的框包围起来的。

如果用户所输入的信息通过了第一个检查,那么我们要检查它是不是一个正数。如果不是,我们还是用form_set_error来显示错误信息。注意错误信息的string是要用t()方法来翻译一下的。
 

 

 

<?php

//帮助
function hellodrupal_help($path) {
            $output = ''; //declare your output variable
            switch ($path) {
            case "admin/help#hellodrupal":
            $output = '<p>'. t("第一个Drupal模块") .'</p>';
             break;
            }
             return $output;
}//function on this display help

function hellodrupal_block($op='list',$delta=0){
    
    switch ($op)
    {
        case 'list' :
        $block[0]['info'] = t('我的第一个drupal模块');
        $block[1]['info'] = t('我的第二个drupal模块');
        return $block;
        
        case 'view' :
          switch ($delta)
          {
             case 0 :
               $block['subject'] = '我的第一个block';
             $block['content'] = '欢迎访问我的站点';
             break;
             case 1 :
               //$block['subject'] = '我的第二个block';
             //$block['content'] = '欢迎再次访问我的站点';
             $query = "select nid,title,created from {node}";
             $queryResult = db_query_range($query, 0 ,10);
             $links = array();
             while ($node = db_fetch_object($queryResult))
             {
                 $links[] = l($node->title,'node/'.$node->nid);
             }
             $block['content'] = theme('item_list', $links);
             $block['subject'] = '我的第二个block';
             break;
          }
        return $block;
        break;
    }
}
function hellodrupal_menu() {
    $items = array();
    $items['hello'] = array(
    'title'=>t('hello drupal page'),
    'page callback'=>'hello_druapl_page',
    'page arguments' => array('hello','helloDrupal'),
    //'page arguments' => array(1,2),
    //它的优先级 大于 url传来的参数 也就是此时url如果放参数,参数不起作用 
    //如果要默认值 就放在回调函数的形参上  (默认的时候要注意,第几个有默认值, page arguments=>array(......)) 中第几个元素就必须没有,假如有的话 虽不在url中赋值,但是是有值的 值为两个单或双引号括起来 
    //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
    'access arguments'=> array('access hellodrupal blocks')  
    );
    $items['helloform'] = array(
    'title'=>t('hello drupal page form'),
    'page callback'=>'hello_drupal_form',
    'page arguments' => array('page callback arguments'),
    'access arguments'=> array('access hellodrupal blocks')  
    );
    
    return $items;    

}


/*function hello_druapl_page()
{
  $output = 'hello drupal page';
  return $output;
}*/
function hello_druapl_page($name = null, $nickname = null)
{
      $output = '';
    if(empty($name)){
      $output = "hello andbody";
    }
    else {
      $output = "hello ".$name;
    }
    if(!empty($nickname)){
      $output .= ' your nick name is '.$nickname;
    }
    return $output;
}
//定义权限 仅仅指明什么权限对这个模块可用
function hellodrupal_perm(){
      return array('access hellodrupal blocks');
}

function hello_drupal_form(){
  return drupal_get_form('my_form');
}
/*function my_form()//my_form 就是 $form_id
{

    // 文本框  name是文本框的名称
    $form['name'] =  array(
    '#type' =>'textfield',//drupal 规定文本框用textfield来表示
    '#title'=>t('Your name'),//标题 显示在文本框前面
    '#description'=>t('Please input your name')//文本框下面的描述
   );
   //提交按钮  //
  $form['submit'] = array('#type'=>'submit','#value'=>t('Save'));
  return $form;
}*/
function my_form()//my_form 就是 $form_id
{
    $form['basic'] = array(
        '#type'=>'fieldset',
        '#title'=>t('Basic information'),
        '#collapsible'=>true,
        '#collapsed'=>false
        );

    // 文本框  name是文本框的名称
    $form['basic']['name'] =  array(
        '#type' =>'textfield',//drupal 规定文本框用textfield来表示
        '#title'=>t('Your name'),//标题 显示在文本框前面
        '#default_value'=>'hello world',
        '#description'=>t('Please input your name')//文本框下面的描述
    );
    $form['basic']['age'] =  array(
    '#type'=>'textfield',
    '#title'=>'Your age'
    );
   //提交按钮  //
  $form['submit'] = array('#type'=>'submit','#value'=>t('Save'));
  return $form;
}

//表单函数_validate 验证类似于钩子函数的回调函数
function my_form_validate(&$form, &$form_state)  //my_form 就是 $form_id
{
    /**
   * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
   */
    // form_set_error('',$form_values['name']);
  if($form_state['values']['name'] == '')
  {
    form_set_error('',t('You must input your name'));
  }
}
//表单函数_submit 提交函数
function my_form_submit(&$form, &$form_state)//my_form 就是 $form_id
{
     /**
       * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
       */
     drupal_set_message(t('Your name is ').$form_state['values']['name'].'    '.
     t('Your age is ').$form_state['values']['age']);
}

 

 

 

 

 

 

 

 

 

普通分类: