HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file formrule.php

Documentation is available at formrule.php

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

Documentation generated on Mon, 11 Mar 2019 14:46:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.