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

  1. <?php
  2. /**
  3. * Examples of usage for HTML_QuickForm: fancy validation with addFormRule()
  4. *
  5. * $Id: formrule.php,v 1.1 2003/12/20 21:05:32 avb Exp $
  6. * @author      Alexey Borzov <avb@php.net>
  7. * @version     3.2
  8. */
  9.  
  10. require_once 'HTML/QuickForm.php';
  11.  
  12. function _validate_shipping($values)
  13. {
  14.     // In Real Life (tm) you will probably query your DB for these
  15.     $profiles = array('foo', 'bar', 'baz');
  16.     $errors   = array();
  17.     switch ($values['profile']) {
  18.         case 'personal': 
  19.             if (empty($values['persProfileName'])) {
  20.                 $errors['persProfileName'] = 'Enter the profile name';
  21.             } elseif (in_array($values['persProfileName'], $profiles)) {
  22.                 $errors['persProfileName'] = 'The profile already exists';
  23.             }
  24.             if (empty($values['persName']['first']) || empty($values['persName']['last'])) {
  25.                 $errors['persName'] = 'Name is required';
  26.             }
  27.             if (empty($values['persAddress'])) {
  28.                 $errors['persAddress'] = 'Address is required';
  29.             }
  30.             break;
  31.  
  32.         case 'company': 
  33.             if (empty($values['compProfileName'])) {
  34.                 $errors['compProfileName'] = 'Enter the profile name';
  35.             } elseif (in_array($values['compProfileName'], $profiles)) {
  36.                 $errors['compProfileName'] = 'The profile already exists';
  37.             }
  38.             if (empty($values['compName'])) {
  39.                 $errors['compName'] = 'Company name is required';
  40.             }
  41.             if (empty($values['compAddress'])) {
  42.                 $errors['compAddress'] = 'Address is required';
  43.             }
  44.             break;
  45.  
  46.         case 'existing': 
  47.         default:
  48.             if (empty($values['profileName'])) {
  49.                 $errors['profileName'] = 'Enter the profile name';
  50.             } elseif (!in_array($values['profileName'], $profiles)) {
  51.                 $errors['profileName'] = 'The profile does not exist';
  52.             }
  53.             break;
  54.     } // switch
  55.     return empty($errors)? true: $errors;
  56. }
  57.  
  58. $form =& new HTML_QuickForm('frmFancy');
  59. $form->setDefaults(array(
  60.     'profile'     => 'existing',
  61.     'stuffAmount' => '1'
  62. ));
  63. $renderer =& $form->defaultRenderer();
  64. $renderer->setElementTemplate("\n\t<tr>\n\t\t<td style=\"white-space: nowrap; background-color: #F0F0F0;\" align=\"left\" valign=\"top\" colspan=\"2\"><b>{element}</b></td>\n\t</tr>", 'profile');
  65.  
  66. $form->addElement('header', null, 'Choose stuff');
  67. $form->addElement('select', 'stuffName', 'Stuff to send:', array('' => '--select--', 'n' => 'Nuts', 'b' => 'Bolts', 'f' => 'Flotsam', 'j' => 'Jetsam'));
  68. $form->addElement('text',   'stuffAmount', 'Amount of stuff:', array('size' => 2, 'maxlength' => 2));
  69.  
  70.  
  71. $form->addElement('header', null, 'Choose shipping profile');
  72. $form->addElement('static', 'note', 'Note:', 'profiles \'foo\', \'bar\' and \'baz\' are considered existing');
  73.  
  74. $form->addElement('radio',  'profile', null, 'Use existing profile', 'existing');
  75. $form->addElement('text',   'profileName', 'Profile name:', array('size' => 32, 'maxlength' => 32));
  76.  
  77. $form->addElement('radio',  'profile', null, 'New personal profile', 'personal');
  78. $form->addElement('text',   'persProfileName', 'Profile name:', array('size' => 32, 'maxlength' => 32));
  79. $name[] =& $form->createElement('text', 'first', null, array('size' => 14, 'maxlength' => 100));
  80. $name[] =& $form->createElement('text', 'last', null, array('size' => 14, 'maxlength' => 100));
  81. $form->addGroup($name, 'persName', 'Name (first, last):', ' ');
  82. $form->addElement('text',   'persAddress', 'Address:', array('size' => 32, 'maxlength' => 255));
  83.  
  84. $form->addElement('radio',  'profile', null, 'New company profile', 'company');
  85. $form->addElement('text',   'compProfileName', 'Profile name:', array('size' => 32, 'maxlength' => 32));
  86. $form->addElement('text',   'compName', 'Company name:', array('size' => 32, 'maxlength' => 100));
  87. $form->addElement('text',   'compAddress', 'Address:', array('size' => 32, 'maxlength' => 255));
  88.  
  89. $form->addElement('submit', null, 'Send');
  90.  
  91. $form->addFormRule('_validate_shipping');
  92.  
  93. if ($form->validate()) {
  94.     echo "<pre>\n";
  95.     var_dump($form->exportValues());
  96.     echo "</pre>\n";
  97. }
  98.  
  99. $form->display();
  100. ?>
  101.