pear::QuickForm

<?php
  /**
   * QuickForm手工篇
   *
   * QuickForm手工輸入之熟練篇
   * 用QuickForm完成一個用戶註冊頁面(包括信息的審查功能)
   * @author jxyuhua at gmail.com
   */
   //require_once('move.js');
   require_once('HTML/QuickForm.php');
   echo('<link rel="stylesheet" href="css.css" />');
  
   $quickForm = new HTML_QuickForm('registerFrm1');
   $country  = array('1' => '中國',
                    '2' => '日本',
                    '3' => '美國',
                    '4' => '英國',
                    '0' => '其它');
   $city     = array('1' => '北京',
                    '2' => '廣東',
                    '3' => '江西',
                    '0' => '其它');
   $industry = array('1' => '學生',
                    '2' => '科研機構',
                    '3' => 'IT產業',
                    '0' => '其它');
   $from     = array('1' => '網吧',
                    '2' => '單位',
                    '3' => '家裏',
                    '0' => '其它');
   $quickForm->setDefaults(array('idtype' => 1,
                                 'secret' => 1));
   $quickForm->addElement('header', null, '新用戶註冊');
   $quickForm->addElement('text', 'name', '用戶登錄呢稱:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('password', 'pass', '密碼:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('password', 'repass', '重複輸入密碼:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('text', 'email', 'EMAIL地址:', array('size' => 20, 'maxlength' => 50));
   $quickForm->addElement('text', 'passquestion', '密碼提示問題:', array('size' => 50, 'maxlength' => 50));
   $quickForm->addElement('text', 'passanswer', '密碼提示答案:', array('size' => 50, 'maxlength' => 50));
   $quickForm->addElement('text', 'alias', '用戶中文呢稱:', array('size' => 30, 'maxlength' => 50));
   $quickForm->addElement('textarea', 'description', '個人描述信息:', array('rows' => 3, 'cols' => 50, 'class' => 'textBox'));
   $radio[] = &$quickForm->createElement('radio', null, null, '以下信息對不外公開', '1');
   $radio[] = &$quickForm->createElement('radio', null, null, '以下信息對外公開', '0');
   $quickForm->addGroup($radio, 'secret');
   //?如何設置默認選中爲身份證?
   //中轉的辦法,設默認值
   $quickForm->addElement('text', 'idnuber', '證件號碼:', array('size' => 30, 'maxlength' => 30));
   $radio2[] =  $quickForm->createElement('radio', 'idtype', null, '身份證', null, array('value' => 1, 'checked' => 'true'));
   $radio2[] =  $quickForm->createElement('radio', 'idtype', null, '其它證件', null, array('value' => 2));
   $quickForm->addGroup($radio2);
   $quickForm->addElement('text', 'realname', '真實姓名:', array('size' => 20, 'maxlength' => 30));
   $quickForm->addElement('select', 'gender', '性別:', array('male' => '男', 'female' => '女'));
   $quickForm->addElement('date', 'born', '出生於:', array('format' => 'Y 年m 月d', 'minYear' => 1940, 'maxYear' => 1995));
   $quickForm->addElement('select', 'country', '所在國家:', $country);
   $quickForm->addElement('select', 'city', '省(市):', $city);
   $quickForm->addElement('text', 'town', '市(縣):', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('text', 'address', '聯繫地址:', array('size' => 50, 'maxlength' => 50));
   $quickForm->addElement('text', 'zipcode', '郵政編碼:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('text', 'phone', '聯繫電話:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('text', 'company', '所在單位:', array('size' => 30, 'maxlength' => 30));
   $quickForm->addElement('text', 'department', '所在部門:', array('size' => 20, 'maxlength' => 20));
   $quickForm->addElement('select', 'industry', '所在行業:', $industry);
   $quickForm->addElement('select', 'source', '主要在哪裏<br />訪問我們的網站:', $from);
   $quickForm->addElement('text', 'webpage', '個人主頁:', array('size' => 50, 'maxlength' => 50));
   $quickForm->addElement('submit', null, '註冊用戶');

   //設定表單的規則
   $quickForm->applyFilter('name', 'trim');
   $quickForm->addRule('name', '用戶登錄暱稱必須填寫!!', 'required');
   $quickForm->addRule('pass', '密碼不能爲空', 'required');
   $quickForm->addRule('email', '電子郵件EMAIL不能爲空', 'required');   
   $quickForm->addRule('passquestion', '請填寫密碼提示問題', 'required');
   $quickForm->addRule('passanswer', '請填寫密碼提示答案', 'required');
   $quickForm->addRule('idnuber', '請填寫證件號碼', 'required');
   $quickForm->addRule('realname', '請填寫真實姓名', 'required');
   $quickForm->addRule('gender', '性別不能爲空', 'required');
   $quickForm->addRule('born', '出生日期不能爲空', 'required');
   $quickForm->addRule('country', '國家不能爲空', 'required');
   $quickForm->addRule('city', '城市不能爲空', 'required');
   $quickForm->addRule('address', '請填寫你的聯繫地址', 'required');
   
   $quickForm->addRule('name', '用戶登錄呢稱最少爲5個字符', 'minlength', 5);
   $quickForm->addRule('pass', '密碼太簡單,不能少於5位', 'minlength', 5);
   $quickForm->addRule(array('pass', 'repass'), '兩次的密碼不一致', 'compare');
   $quickForm->addRule('email', '請輸入正確的EMAIL地址(user@domain)', 'email');
   $quickForm->addRule('idnuber', '證件號碼不能少於5位', 'minlength', 5);
   $quickForm->addRule('idnuber', '證件號碼只能是英文字母或數字', 'alphanumeric');
   $quickForm->registerRule('keyword', 'function', 'checkUser');
   $quickForm->addRule('name', '用戶名已經存在,請重新選擇', 'keyword');

   //change style
   $headerTemplate = <<< HTML
<tr>
<td style="white-space: nowrap; background-color: #CCCCCC;" align="center" valign="top" colspan="2"><b>{header}</b></td>
</tr>
HTML;
   $formTemplate = <<< HTML
<form{attributes}>
<table border="0" cellspacing="1" cellpadding="0" class="table1" align="center">
{content}
</table>
</form>
HTML;
   $elementTemplate = <<< HTML
<tr>
<td align="right" valign="top" class="td7"><!-- BEGIN required --><span style="color: #ff0000">*</span><!-- END required -->{label}</td>
<td valign="top" align="left" class="td8"><!-- BEGIN error --><span style="color: #ff0000">{error}</span><br /><!-- END error --> {element}</td>
</tr>   
HTML;
   $render = & $quickForm->defaultRenderer();
   $render->setHeaderTemplate($headerTemplate);
   $render->setFormTemplate($formTemplate);
   $render->setElementTemplate($elementTemplate);


   if($quickForm->validate()) {
       $quickForm->process('exportValues');    
   } else {
       $quickForm->display();    
   }

   function exportValues($data) {
       echo('<pre>');
       print_r($data);
       echo('</pre>');
   }   
   
   function checkUser($elementName, $elementValue) {
       $keywords = array('admin', 'administrator', 'webmaster', 'supervisor');
       if(in_array($elementValue, $keywords)) {
           Return false;    
       } else {
              Return true;
       }
   }

//$quickForm->addElement('password', 'repass', '重複輸入密碼:', array('size' => 20, 'maxlength' => 20));

//可通過加'value'=>123 獲得默認值
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章