home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / regWizard.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.8 KB  |  148 lines

  1. <?php
  2. /**
  3.  * Example for HTML_QuickForm_Controller: registration wizard
  4.  * 
  5.  * $Id: regWizard.php,v 1.2 2003/10/02 12:49:38 avb Exp $
  6.  * 
  7.  * @author Bertrand Mansion <bmansion@mamasam.com>
  8.  */
  9.  
  10. require_once 'HTML/QuickForm/Controller.php';
  11. require_once 'HTML/QuickForm/Action/Display.php';
  12.  
  13. // Start the session
  14.  
  15. session_start();
  16.  
  17. // Rule for passwords comparison
  18. function comparePassword($fields)
  19. {
  20.     if (strlen($fields['password1']) && strlen($fields['password2']) && 
  21.         $fields['password1'] != $fields['password2']) {
  22.         return array('password1' => 'Passwords are not the same');
  23.     }
  24.     return true;
  25. }
  26.  
  27. // Class for first page : credentials
  28.  
  29. class Page_Account_Credentials extends HTML_QuickForm_Page
  30. {
  31.  
  32.     function buildForm()
  33.     {
  34.         $this->_formBuilt = true;
  35.  
  36.         $this->addElement('header', 'credential_header', 'Your credentials');
  37.         
  38.         $this->addElement('text', 'username', 'Your email address :', array('size' => 30, 'maxlength' => 63));
  39.         $this->addElement('password', 'password1', 'Your password :', array('size' => 16, 'maxlength' => 32));
  40.         $this->addElement('password', 'password2', 'Confirm your password :', array('size' => 16, 'maxlength' => 32));
  41.         
  42.         $buttons[0] =& HTML_QuickForm::createElement('button', 'cancel', 'Cancel', array('onclick'=>"javascript:location.href='http://pear.php.net/package/HTML_QuickForm';"));
  43.         $buttons[1] =& HTML_QuickForm::createElement('submit', $this->getButtonName('next'), 'Next step >>');
  44.         $this->addGroup($buttons, 'buttons', '', ' ', false);
  45.     
  46.         $this->addRule('username', 'Your email address is required', 'required', null, 'client');
  47.         $this->addRule('username', 'Your email address is incorrect', 'email', null, 'client');
  48.  
  49.         $this->addRule('password1', 'The password is required', 'required', '', 'client');
  50.         $this->addRule('password1', 'The password is too short: 6 chars minimum', 'minlength', 6, 'client');
  51.  
  52.         $this->addRule('password2', 'The password confirmation is required', 'required', '', 'client');
  53.  
  54.         $this->addFormRule('comparePassword');
  55.  
  56.         $this->setDefaultAction('next');
  57.     }
  58. }
  59.  
  60. // Class for second page : user data
  61.  
  62. class Page_Account_Information extends HTML_QuickForm_Page
  63. {
  64.  
  65.     function buildForm()
  66.     {
  67.         $this->_formBuilt = true;
  68.  
  69.         $this->addElement('header', 'data_header', 'Your personal data');
  70.         
  71.         $name[] = &HTML_QuickForm::createElement('text', 'first', 'Firstname', array('size' => 16, 'maxlength' => 63));
  72.         $name[] = &HTML_QuickForm::createElement('text', 'last', 'Lastname', array('size' => 16, 'maxlength' => 63));
  73.         $this->addGroup($name, 'name', 'Your name :', null, false);
  74.         
  75.         $this->addElement('text', 'company', 'Your company :',  array('size' => 30, 'maxlength' => 63));
  76.         
  77.         $this->addElement('text', 'address1', 'Your address :',  array('size' => 30, 'maxlength' => 63));
  78.         $this->addElement('text', 'address2', ' ',  array('size' => 30, 'maxlength' => 63));
  79.         
  80.         $this->addElement('text', 'zip', 'Zip code :',  array('size' => 16, 'maxlength' => 16));
  81.         $this->addElement('text', 'city', 'City :',  array('size' => 30, 'maxlength' => 63));
  82.         
  83.         $countries = array('FR'=>'France', 'GE'=>'Germany', 'RU'=>'Russia', 'UK'=>'United Kingdom');
  84.         $this->addElement('select', 'country', 'Your country :', $countries);
  85.         
  86.         $this->addElement('text', 'phone', 'Phone number :',  array('size' => 16, 'maxlength' => 16));
  87.         $this->addElement('text', 'fax', 'Fax number :',  array('size' => 16, 'maxlength' => 16));
  88.     
  89.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Previous step');
  90.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Finish');
  91.         $this->addGroup($prevnext, 'buttons', '', ' ', false);
  92.         
  93.         $this->addRule('name', 'First and last names are required', 'required');
  94.         $this->addRule('company', 'Company is required', 'required');
  95.         $this->addRule('address1', 'Address is required', 'required');
  96.         $this->addRule('city', 'City is required', 'required');
  97.  
  98.         $this->setDefaultAction('next');
  99.     }
  100. }
  101.  
  102. // Class for form rendering
  103.  
  104. class ActionDisplay extends HTML_QuickForm_Action_Display
  105. {
  106.     function _renderForm(&$page) 
  107.     {
  108.         $renderer =& $page->defaultRenderer();
  109.  
  110.         $page->setRequiredNote('<font color="#FF0000">*</font> shows the required fields.');
  111.         $page->setJsWarnings('Those fields have errors :', 'Thanks for correcting them.');
  112.         
  113.         $renderer->setFormTemplate('<table width="450" border="0" cellpadding="3" cellspacing="2" bgcolor="#CCCC99"><form{attributes}>{content}</form></table>');
  114.         $renderer->setHeaderTemplate('<tr><td style="white-space:nowrap;background:#996;color:#ffc;" align="left" colspan="2"><b>{header}</b></td></tr>');
  115.         $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'name');
  116.         $renderer->setGroupElementTemplate('<td>{element}<br /><span style="font-size:10px;"><!-- BEGIN required --><span style="color: #f00">*</span><!-- END required --><span style="color:#996;">{label}</span></span></td>', 'name');
  117.  
  118.         $page->accept($renderer);
  119.         echo $renderer->toHtml();
  120.     }
  121. }
  122.  
  123. // Class for form processing
  124.  
  125. class ActionProcess extends HTML_QuickForm_Action
  126. {
  127.     function perform(&$page, $actionName)
  128.     {   
  129.         $values = $page->controller->exportValues();
  130.         echo '<pre>';
  131.         var_dump($values);
  132.         echo '</pre>';
  133.     }
  134. }
  135.  
  136.  
  137. $wizard =& new HTML_QuickForm_Controller('regWizard', true);
  138. $wizard->addPage(new Page_Account_Credentials('page1'));
  139. $wizard->addPage(new Page_Account_Information('page2'));
  140.  
  141. $wizard->setDefaults(array('country' => 'FR'));
  142.  
  143. $wizard->addAction('display', new ActionDisplay());
  144. $wizard->addAction('process', new ActionProcess());
  145.  
  146. $wizard->run();
  147. ?>
  148.