| 1 | jonen | 1.1 | <?php | 
| 2 |  |  |  | 
| 3 |  |  | /** | 
| 4 |  |  | * This is an unrealistic example form that uses all available form elements. | 
| 5 |  |  | * No items have explicitly been marked as required, enabling you to test | 
| 6 |  |  | * individual elements by entering good data or garbage and seeing the result. | 
| 7 |  |  | * | 
| 8 |  |  | * $Id: form3.php,v 1.8 2004/03/28 19:46:20 culley Exp $ | 
| 9 |  |  | * | 
| 10 |  |  | * @author Culley Harrelson <culley@fastmail.fm> | 
| 11 |  |  | * @package phpHtmlLib | 
| 12 |  |  | * @subpackage form-examples | 
| 13 |  |  | * @version 1.0.0 | 
| 14 |  |  | * | 
| 15 |  |  | */ | 
| 16 |  |  | function xxx($var) { echo "<xmp>"; var_dump($var); echo "</xmp>"; } | 
| 17 |  |  |  | 
| 18 |  |  | // Include the phphtmllib libraries | 
| 19 |  |  | $phphtmllib = $_SERVER["DOCUMENT_ROOT"] . "/phphtmllib"; | 
| 20 |  |  | include_once("$phphtmllib/includes.inc"); | 
| 21 |  |  |  | 
| 22 |  |  | // Include the Form Processing objects | 
| 23 |  |  | include_once($phphtmllib."/form/includes.inc"); | 
| 24 |  |  |  | 
| 25 |  |  |  | 
| 26 |  |  | //use the class we defined from | 
| 27 |  |  | //Example 3. | 
| 28 |  |  | include_once($phphtmllib."/examples/MyLayoutPage.inc"); | 
| 29 |  |  |  | 
| 30 |  |  | /** | 
| 31 |  |  | * A simple Page Layout object child.  this came from Example 3. | 
| 32 |  |  | * | 
| 33 |  |  | * @author Culley Harrelson <culley@fastmail.fm> | 
| 34 |  |  | * @package phpHtmlLib | 
| 35 |  |  | * @subpackage form-examples | 
| 36 |  |  | * | 
| 37 |  |  | */ | 
| 38 |  |  | class Form3Page extends MyLayoutPage { | 
| 39 |  |  |  | 
| 40 |  |  | function content_block() { | 
| 41 |  |  | //build the FormProcessor, and add the | 
| 42 |  |  | //Form content object that the FormProcessor | 
| 43 |  |  | //will use.  Make the width of the form 95% | 
| 44 |  |  | return new FormProcessor(new SampleForm('95%')); | 
| 45 |  |  | } | 
| 46 |  |  | } | 
| 47 |  |  |  | 
| 48 |  |  |  | 
| 49 |  |  | /** | 
| 50 |  |  | * This is the Class that handles the building of the Form itself.  It creates | 
| 51 |  |  | * the Form Elements inside the form_init_elements() method. | 
| 52 |  |  | * | 
| 53 |  |  | * @author Culley Harrelson <culley@fastmail.fm> | 
| 54 |  |  | * @package phpHtmlLib | 
| 55 |  |  | * @subpackage form-examples | 
| 56 |  |  | * | 
| 57 |  |  | */ | 
| 58 |  |  |  | 
| 59 |  |  | class SampleForm extends FormContent { // {{{ | 
| 60 |  |  |  | 
| 61 |  |  | /** | 
| 62 |  |  | * This method gets called EVERY time the object is created.  It is used to | 
| 63 |  |  | * build all of the FormElement objects used in this Form. | 
| 64 |  |  | * | 
| 65 |  |  | */ | 
| 66 |  |  | function form_init_elements() { | 
| 67 |  |  |  | 
| 68 |  |  | // we want an confirmation page for this form. | 
| 69 |  |  | $this->set_confirm(); | 
| 70 |  |  |  | 
| 71 |  |  | // now start to add the Form Elements that will be used in this form. | 
| 72 |  |  |  | 
| 73 |  |  | // FEButton  (label $label, string $value, [string $action = NULL], | 
| 74 |  |  | // [int $width = NULL], [int $height = NULL]) | 
| 75 |  |  | $this->add_element(new FEButton("FEButton Label", 'button-value', 'javascript-action')); | 
| 76 |  |  |  | 
| 77 |  |  | // FECheckBox  (label $label, string $text) | 
| 78 |  |  | $this->add_element(new FECheckBox("FECheckBox Label", 'checkbox-text')); | 
| 79 |  |  |  | 
| 80 |  |  | // FEDataList  (string $label, [boolean $required = TRUE], [int $width | 
| 81 |  |  | // = NULL], [int $height = NULL], [array $data_list = array()]) | 
| 82 |  |  | $list =  new FECheckBoxList("FECheckBoxList label", FALSE, | 
| 83 |  |  | "200px", "80px", | 
| 84 |  |  | array("Testing 123" => "foo", | 
| 85 |  |  | "my value is bar" => "bar", | 
| 86 |  |  | "Somone's test" => "blah", | 
| 87 |  |  | "Slerm" => "slerm", | 
| 88 |  |  | "my value is hat" => "hat", | 
| 89 |  |  | "One" => 1)); | 
| 90 |  |  | $list->disable_item("Testing 123"); | 
| 91 |  |  | $this->add_element($list); | 
| 92 |  |  |  | 
| 93 |  |  | // FEComboListBox  (string $label, [boolean $required = TRUE], [int | 
| 94 |  |  | // $width = "200px"], [int $height = "100px"], [array $from_data_list = | 
| 95 |  |  | // array()], [array $to_data_list = array()]) | 
| 96 |  |  | $combo_list = new FEComboListBox("FEComboListBox Label", false, '300px', | 
| 97 |  |  | '100px', array('one' => 1, 'two' => 2), array('three' => 3, 'four' => 4)); | 
| 98 |  |  | $combo_list->set_to_label('this is the to label'); | 
| 99 |  |  | $combo_list->set_from_label('this is the from label'); | 
| 100 |  |  | $this->add_element($combo_list); | 
| 101 |  |  |  | 
| 102 |  |  | // FEConfirmActionButton  (mixed $label, mixed $value, [mixed $message | 
| 103 |  |  | // = NULL], [mixed $width = NULL], [mixed $height = NULL]) | 
| 104 |  |  | $this->add_element(new FEConfirmActionButton("FEConfirmActionButton label", | 
| 105 |  |  | 'click me for a javascript confirmation', 'Are you sure?')); | 
| 106 |  |  |  | 
| 107 |  |  | // the constructor for FEPassword and FEConfirmPassword are the same as FEText | 
| 108 |  |  | $password = new FEPassword("FEPassword label", false, "200px"); | 
| 109 |  |  | $this->add_element($password); | 
| 110 |  |  |  | 
| 111 |  |  | $confirm = new FEConfirmPassword("FEConfirmPassword label", false, "200px"); | 
| 112 |  |  |  | 
| 113 |  |  | // add the password FormElement to the ConfirmPassword FormElement so | 
| 114 |  |  | // we can make sure they match. | 
| 115 |  |  | $confirm->password($password); | 
| 116 |  |  | $this->add_element($confirm); | 
| 117 |  |  |  | 
| 118 |  |  | // These elements have the same constructor as FEText | 
| 119 |  |  | $this->add_element(new FEDomainName("FEDomainName label", false, "200px")); | 
| 120 |  |  | $this->add_element(new FEEmail("FEEmail label", false, "200px")); | 
| 121 |  |  | $this->add_element(new FEEmailMany("FEEmailMany label (comma separated)", false, "400px")); | 
| 122 |  |  |  | 
| 123 |  |  | // file upload | 
| 124 |  |  | $file = new FEFile("FEFile label", false, "200px"); | 
| 125 |  |  | $file->add_valid_type('image/gif'); | 
| 126 |  |  | $file->add_valid_type('image/jpeg'); | 
| 127 |  |  | $file->set_max_size(1024 * 2); | 
| 128 |  |  | $this->add_element($file); | 
| 129 |  |  |  | 
| 130 |  |  | // FEHidden  (mixed $label, [mixed $value = NULL]) | 
| 131 |  |  | $this->add_element(new FEHidden("FEHidden label", 'the hidden value')); | 
| 132 |  |  |  | 
| 133 |  |  | // FEHostNameWithPort  (label $label, [bool $required = TRUE], [int | 
| 134 |  |  | // $width = NULL], [int $maxlength = NULL], [bool $seperate_port = | 
| 135 |  |  | // FALSE]) | 
| 136 |  |  | $this->add_element(new FEHostNameWithPort("FEHostNameWithPort label", false)); | 
| 137 |  |  |  | 
| 138 |  |  | // Same constructor as FEText | 
| 139 |  |  | $this->add_element(new FEIPAddress("FEIPAddress label", false, "200px")); | 
| 140 |  |  |  | 
| 141 |  |  | // Same constructor as FEHostNameWithPort | 
| 142 |  |  | $this->add_element(new FEIPAddressWithPort("FEIPAddressWithPort label", false)); | 
| 143 |  |  |  | 
| 144 |  |  | // your standard drop down select list | 
| 145 |  |  | $list_box_collapsed = new FEListBox('FEListBoxCollapsed label', false, '200px'); | 
| 146 |  |  | $list_box_collapsed->set_list_data(array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4)); | 
| 147 |  |  | $this->add_element($list_box_collapsed); | 
| 148 |  |  |  | 
| 149 |  |  | // same as the above select list but with a height setting | 
| 150 |  |  | $list_box = new FEListBox('FEListBox label', false, '200px', '100px'); | 
| 151 |  |  | $list_box->set_list_data(array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4)); | 
| 152 |  |  | $this->add_element($list_box); | 
| 153 |  |  |  | 
| 154 |  |  | // FEMonths($label, $required = TRUE, $width = NULL, $height = NULL, $locale = 'en', $format = 'long') { | 
| 155 |  |  | $this->add_element(new FEMonths("FEMonths label", false)); | 
| 156 |  |  |  | 
| 157 |  |  | // FEYears($label, $required = TRUE, $width = NULL, $height = NULL, $min_year = 2000, $max_year = 2010) { | 
| 158 |  |  | $this->add_element(new FEYears("FEYears label", false, null, null, date('Y'), date('Y') + 10)); | 
| 159 |  |  |  | 
| 160 |  |  | // FEDays($label, $required = TRUE, $width = NULL, $height = NULL) { | 
| 161 |  |  | $this->add_element(new FEDays("FEDays label", false)); | 
| 162 |  |  |  | 
| 163 |  |  | // set the locale to dutch | 
| 164 |  |  | setlocale(LC_TIME, 'nl_NL'); | 
| 165 |  |  |  | 
| 166 |  |  | // TODO: document this | 
| 167 |  |  | $date_element = new FEDate("FEDate label", false, null, null, 'Fdy', 1970, 1975); | 
| 168 |  |  | //        $date_element->set_short_months(); | 
| 169 |  |  | //        $date_element->set_min_year(1970); | 
| 170 |  |  | //        $date_element->set_max_year(1975); | 
| 171 |  |  | //        $date_element->set_format('Fdy'); | 
| 172 |  |  | $date_element->set_text_format("%s %s, %s"); | 
| 173 |  |  | //        $date_element->set_text_format("%04d-%02d-%02d"); | 
| 174 |  |  | $this->add_element($date_element); | 
| 175 |  |  |  | 
| 176 |  |  | // a list box that allows you to select multiple items | 
| 177 |  |  | $m_list_box = new FEMultiListBox('FEMultiListBox label', false, '200px', '100px'); | 
| 178 |  |  | $m_list_box->set_list_data(array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4)); | 
| 179 |  |  | $this->add_element($m_list_box); | 
| 180 |  |  |  | 
| 181 |  |  | // Same constructor as FEText | 
| 182 |  |  | $this->add_element(new FEName("FEName label", false, "200px")); | 
| 183 |  |  |  | 
| 184 |  |  | $nested_list_box = new FENestedListBox('FENestedListBox label', false, '200px', '100px'); | 
| 185 |  |  | $data = array("Test" => 1, "Foo" => array("value" => 2, "items" => array("Blah" => 3, "php" => 4)), "Bar" => array("value" => 5, "items" => array("testing" => array("value" => 6, "items" => array("ugh" => 7)), "again" => 8))); | 
| 186 |  |  | $nested_list_box->set_list_data($data); | 
| 187 |  |  | $this->add_element($nested_list_box); | 
| 188 |  |  |  | 
| 189 |  |  | // Same constructor as FEText | 
| 190 |  |  | $this->add_element(new FENumber("FENumber label", false, "200px")); | 
| 191 |  |  | $this->add_element(new FENumberFloat("FENumberFloat label", false, "200px")); | 
| 192 |  |  | $this->add_element(new FENumberFloat("FENumberFloat label", false, "200px")); | 
| 193 |  |  |  | 
| 194 |  |  | // FENumberInRange  (label $label, [bool $required = TRUE], [int $width | 
| 195 |  |  | // = NULL], [int $maxlength = NULL], int $min, [int $max = 100], | 
| 196 |  |  | // [boolean $label_flag = TRUE]) | 
| 197 |  |  | $this->add_element(new FENumberInRange("FENumberInRange label", false, "200px", null, 1, 10)); | 
| 198 |  |  |  | 
| 199 |  |  |  | 
| 200 |  |  | // Same constructor as FEText | 
| 201 |  |  | $this->add_element(new FENumberPrice("FENumberPrice label", false, "200px")); | 
| 202 |  |  |  | 
| 203 |  |  | // FERadioGroup  (label $label, [array $data_list = array()]) | 
| 204 |  |  | $this->add_element(new FERadioGroup("FERadioGroup label", | 
| 205 |  |  | array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4))); | 
| 206 |  |  |  | 
| 207 |  |  | // this is the same as the above radio group but it is handled | 
| 208 |  |  | // differently in the display method below | 
| 209 |  |  | $this->add_element(new FERadioGroup("FERadioGroup vertical label", | 
| 210 |  |  | array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4))); | 
| 211 |  |  |  | 
| 212 |  |  | // add constructor label here. | 
| 213 |  |  | $this->add_element( new FERegEx("FERegEx label", false, '200px', 3, | 
| 214 |  |  | '/^1[a-z]T$/', 'This field must equal 1[a-z]T') ); | 
| 215 |  |  |  | 
| 216 |  |  | $this->add_element(new FEText("FEText label", false, "200px")); | 
| 217 |  |  |  | 
| 218 |  |  | // FETextArea  (label $label, [bool $required = TRUE], int $rows, int | 
| 219 |  |  | // $cols, [int $width = NULL], [int $height = NULL], [int | 
| 220 |  |  | // $limit_char_count = -1]) | 
| 221 |  |  | $this->add_element(new FETextArea("FETextArea label", false, 10, 50, null, 300)); | 
| 222 |  |  |  | 
| 223 |  |  | // FEUnitedStates  (string $label, [boolean $required = TRUE], [int | 
| 224 |  |  | // $width = NULL], [int $height = NULL], array 4) | 
| 225 |  |  | $this->add_element(new FEUnitedStates("FEUnitedStates label", false)); | 
| 226 |  |  |  | 
| 227 |  |  | // Same constructor as FEText | 
| 228 |  |  | $this->add_element(new FEUrl("FEUrl label", false, "200px")); | 
| 229 |  |  | $this->add_element(new FEUrlStrict("FEUrlStrict label", false, "200px")); | 
| 230 |  |  |  | 
| 231 |  |  | // FEYesNoListBox  (label $label, [bool $required = TRUE], [array | 
| 232 |  |  | // $width = NULL], [int $height = NULL], [string $yes_value = "yes"], | 
| 233 |  |  | // [string $no_value = "no"]) | 
| 234 |  |  | $this->add_element(new FEYesNoListBox("FEYesNoListBox label", false)); | 
| 235 |  |  |  | 
| 236 |  |  | // FEYesNoRadioGroup  (label $label, [bool $required = TRUE], [string | 
| 237 |  |  | // $yes_value = "yes"], [string $no_value = "no"]) | 
| 238 |  |  | $this->add_element(new FEYesNoRadioGroup("FEYesNoRadioGroup label", false)); | 
| 239 |  |  |  | 
| 240 |  |  | // FEZipcode  (label $label, [bool $required = false], [int $width = | 
| 241 |  |  | // NULL], [int $maxlength = 5]) | 
| 242 |  |  | $this->add_element(new FEZipcode("FEZipcode label", false)); | 
| 243 |  |  |  | 
| 244 |  |  | // FESubmitButton  (label $label, string $value, [int $width = NULL], | 
| 245 |  |  | // [int $height = NULL]) | 
| 246 |  |  | $this->add_element(new FESubmitButton("FESubmitButton label", 'submit button value')); | 
| 247 |  |  |  | 
| 248 |  |  | } | 
| 249 |  |  |  | 
| 250 |  |  | /** | 
| 251 |  |  | * This method is called only the first time the form page is hit.  This | 
| 252 |  |  | * enables u to query a DB and pre populate the FormElement objects with | 
| 253 |  |  | * data. | 
| 254 |  |  | * | 
| 255 |  |  | */ | 
| 256 |  |  | function form_init_data() { | 
| 257 |  |  | //this sets the value to the FERadioGroup first value to 3 or 'three' | 
| 258 |  |  | $this->set_element_value('FERadioGroup label', 3); | 
| 259 |  |  |  | 
| 260 |  |  | //this sets the value to the FERadioGroup vertical to 2 or 'two' | 
| 261 |  |  | $this->set_element_value('FERadioGroup vertical label', 2); | 
| 262 |  |  |  | 
| 263 |  |  | //this sets the default value of the FEYesNoRadioGroup to no | 
| 264 |  |  | $this->set_element_value('FEYesNoRadioGroup label', 'no'); | 
| 265 |  |  |  | 
| 266 |  |  | //the default values for check boxes take a boolean | 
| 267 |  |  | $this->set_element_value('FECheckBox Label', true); | 
| 268 |  |  |  | 
| 269 |  |  | //the default values for FEDate elements should be an ISO 8601 date string | 
| 270 |  |  | $this->set_element_value('FEDate label', '1974-10-07'); | 
| 271 |  |  | } | 
| 272 |  |  |  | 
| 273 |  |  |  | 
| 274 |  |  | /** | 
| 275 |  |  | * This is the method that builds the layout of where the FormElements will | 
| 276 |  |  | * live.  You can lay it out any way you like. | 
| 277 |  |  | * | 
| 278 |  |  | */ | 
| 279 |  |  | function form() { | 
| 280 |  |  |  | 
| 281 |  |  | $table = &html_table($this->_width,0,0,2); | 
| 282 |  |  |  | 
| 283 |  |  | // add each element to the form as it was included above | 
| 284 |  |  | foreach(array_keys($this->_elements) as $label) { | 
| 285 |  |  | switch ($label) { | 
| 286 |  |  | case 'FERadioGroup vertical label': | 
| 287 |  |  | // a radio group can be displayed vertically by changing | 
| 288 |  |  | // the br flag on get_element to true | 
| 289 |  |  | $element =& $this->get_element($label); | 
| 290 |  |  | $table->add_row($this->element_label($label), $element->get_element(null, true)); | 
| 291 |  |  | break; | 
| 292 |  |  | default: | 
| 293 |  |  | $table->add_row($this->element_label($label), $this->element_form($label)); | 
| 294 |  |  | break; | 
| 295 |  |  | } | 
| 296 |  |  | } | 
| 297 |  |  |  | 
| 298 |  |  |  | 
| 299 |  |  | return $table; | 
| 300 |  |  | } | 
| 301 |  |  |  | 
| 302 |  |  | /** | 
| 303 |  |  | * This method gets called after the FormElement data has passed the | 
| 304 |  |  | * validation.  This enables you to validate the data against some backend | 
| 305 |  |  | * mechanism, say a DB. | 
| 306 |  |  | * | 
| 307 |  |  | */ | 
| 308 |  |  | function form_backend_validation() { | 
| 309 |  |  | //$this->add_error("uh oh", "some bogus error happened"); | 
| 310 |  |  | //return FALSE; | 
| 311 |  |  | return TRUE; | 
| 312 |  |  | } | 
| 313 |  |  |  | 
| 314 |  |  | /** | 
| 315 |  |  | * This method is called ONLY after ALL validation has passed.  This is the | 
| 316 |  |  | * method that allows you to do something with the data, say insert/update | 
| 317 |  |  | * records in the DB. | 
| 318 |  |  | * | 
| 319 |  |  | */ | 
| 320 |  |  | function form_action() { | 
| 321 |  |  | //$this->add_error("uh oh", "some bogus error happened"); | 
| 322 |  |  | //return FALSE; | 
| 323 |  |  | $this->set_action_message("WOO!"); | 
| 324 |  |  | $dump = html_xmp(); | 
| 325 |  |  | foreach( $this->_elements as $label => $element ) { | 
| 326 |  |  | $dump->add( $label."  = ".print_r($element->get_value(),true) ); | 
| 327 |  |  | } | 
| 328 |  |  |  | 
| 329 |  |  | print $dump->render(); | 
| 330 |  |  | exit; | 
| 331 |  |  | return TRUE; | 
| 332 |  |  | } | 
| 333 |  |  | } //}}} | 
| 334 |  |  |  | 
| 335 |  |  |  | 
| 336 |  |  | $page = new Form3Page("Form Example 3"); | 
| 337 |  |  | print $page->render(); | 
| 338 |  |  |  | 
| 339 |  |  | ?> |