root/trunk/examples/example_element_date.php
| Revision 176, 3.8 kB (checked in by argh, 4 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | <?php |
| 2 | /** |
| 3 | * Example for the Date element |
| 4 | * |
| 5 | * $Id$ |
| 6 | * |
| 7 | * @access public |
| 8 | * @package patForms |
| 9 | * @subpackage Examples |
| 10 | * @author Sebastian Mordziol <argh@php-tools.net> |
| 11 | * @license LGPL, see license.txt for details |
| 12 | * @link http://www.php-tools.net |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Main examples prepend file, needed *only* for the examples framework! |
| 17 | */ |
| 18 | include_once 'patExampleGen/prepend.php'; |
| 19 | $exampleGen->displayHead( 'Example' ); |
| 20 | |
| 21 | |
| 22 | // EXAMPLE START ------------------------------------------------------ |
| 23 | |
| 24 | /** |
| 25 | * main patForms class |
| 26 | */ |
| 27 | require_once $neededFiles['patForms']; |
| 28 | |
| 29 | /** |
| 30 | * patErrorManager class |
| 31 | */ |
| 32 | require_once $neededFiles['patErrorManager']; |
| 33 | |
| 34 | /** |
| 35 | * Attribute notes: |
| 36 | * |
| 37 | * - each token in the date format is automatically replaced |
| 38 | * by a corresponding form element. Unknown tokens are |
| 39 | * regarded as CDATA by the internal parser. For a list of |
| 40 | * supported date tokens, see the $tokensIndex property of |
| 41 | * the date element. |
| 42 | * |
| 43 | * - WARNING: due to a bug in PHP5 with the strtotime function |
| 44 | * (see http://bugs.php.net/bug.php?id=28209), the [now] |
| 45 | * directive will not include the current time, e.g. now will |
| 46 | * return a date like 12.05.2004 00:00:00. I recommend using |
| 47 | * timestamps directly when possible. |
| 48 | * |
| 49 | * - the returnformat attribute can be used to select the kind |
| 50 | * of value the date element will return. Possible values are |
| 51 | * [isodate], [timestamp]. Default: [isodate] |
| 52 | */ |
| 53 | $elementsDefinition = array( |
| 54 | 'xmess' => array( |
| 55 | 'type' => 'Date', |
| 56 | 'attributes' => array( |
| 57 | //'required' => 'yes', |
| 58 | 'display' => 'yes', |
| 59 | 'edit' => 'yes', |
| 60 | 'label' => 'X-Mess eve', |
| 61 | 'title' => 'X-Mess', |
| 62 | 'description' => 'Tell me when you celebrate x-mess.', |
| 63 | 'dateformat' => 'Y.m.d. H:i:s', |
| 64 | 'presets' => 'no', // use select boxes? yes|no |
| 65 | 'default' => time(), // timestamp, ISO 8601 date or strtotime-compatible string |
| 66 | 'min' => '-1 year', // timestamp, ISO 8601 date or strtotime-compatible string |
| 67 | 'max' => '+10 years', // timestamp, ISO 8601 date or strtotime-compatible string |
| 68 | //'returnformat' => 'timestamp' // 'timestamp' or 'isodate' default: 'isodate' => ISO 8601 date |
| 69 | ), |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | // create the form |
| 74 | $form =& patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) ); |
| 75 | |
| 76 | // get the element to have some additonal fun with it |
| 77 | $el =& $form->getElementByName( 'xmess' ); |
| 78 | |
| 79 | // If you want to retrieve the date in the date format you specified, |
| 80 | // you can use the following method: |
| 81 | //echo 'Formatted date: '.$el->getFormattedDate().'<p/>'; |
| 82 | |
| 83 | // When using the set methods for the default, max and min dates, |
| 84 | // you can also use a PEAR::Date object as parameter. |
| 85 | //$el->setMax( new Date( strtotime( '+10 years' ) ) ); |
| 86 | |
| 87 | // If you can't remember what the ISO date format is, simply use the |
| 88 | // provided method, getISOFormat() to get a format string for use with |
| 89 | // PHP's date() function. |
| 90 | //echo 'ISO date: '.date( $el->getISOFormat(), time() ); |
| 91 | |
| 92 | |
| 93 | // create the needed renderer |
| 94 | $renderer =& patForms::createRenderer( "Array" ); |
| 95 | |
| 96 | // set the renderer |
| 97 | $form->setRenderer( $renderer ); |
| 98 | |
| 99 | // use auto-validation |
| 100 | $form->setAutoValidate( 'save' ); |
| 101 | |
| 102 | // serialize the elements |
| 103 | $elements = $form->renderForm(); |
| 104 | |
| 105 | // ERROR DISPLAY ------------------------------------------------------ |
| 106 | // ask the form if it has been submitted and display errors. For |
| 107 | // convenience and also to keep the examples easy to understand, all |
| 108 | // the following examples will use teh helper methods of the examples |
| 109 | // framework to display the errors and the form. |
| 110 | if( $form->isSubmitted() ) |
| 111 | { |
| 112 | displayErrors( $form ); // see patExampleGen/customFunctions.php |
| 113 | } |
| 114 | |
| 115 | // DISPLAY FORM ------------------------------------------------------ |
| 116 | displayForm( $form, $elements ); // see patExampleGen/customFunctions.php |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | // EXAMPLE END ------------------------------------------------------ |
| 122 | $exampleGen->displayFooter(); |
| 123 | ?> |
Note: See TracBrowser for help on using the browser.
