root/tags/RELEASE_0_9_0B1/examples/example_datasrc_object.php

Revision 117, 3.5 kB (checked in by argh, 4 years ago)

Massive update of the examples collection; added the patExampleGen examples framework; commented all examples; reworked all the templates...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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     // element definitions for this example
36     $elementsDefinition = array(
37         'area' => array(
38             'type' => 'Enum',
39             'attributes' => array(
40                 'required'        =>    'yes',
41                 'display'        =>    'yes',
42                 'edit'            =>    'yes',
43                 'label'            =>    'Area',
44                 'title'            =>    'Area',
45                 'style'            =>    'width:200px;',
46                 'description'    =>    'Choose the area to access here.',
47                 'values'        =>    array(),
48             ),
49         ),
50     );
51     
52     // create the form
53     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
54     
55     
56     // DATASOURCE SETUP ------------------------------------------------------
57     /**
58      * Very simple example for a datasource class for patForms elements that support
59      * external data sources, like the Enum element.
60      *
61      * $Id$
62      *
63      * @access        public
64      * @package        patForms
65      * @subpackage    Examples
66      * @author        Sebastian Mordziol <argh@php-tools.net>
67      * @license        LGPL, see license.txt for details
68      * @link        http://www.php-tools.net
69      * @see            example.php
70      */
71     class exampleDatasrc
72     {
73         var $values    =    array(
74             array(   
75                 "label" => "Please select...",
76                 "value"    =>    "",
77             ),
78             array(   
79                 "label" => "-----------",
80                 "value"    =>    "",
81             ),
82             array(   
83                 "label" => "Private Area",
84                 "value"    =>    "a1",
85             ),
86             array(   
87                 "label" => "Area 51",
88                 "value"    =>    "a2",
89             ),
90             array(   
91                 "label" => "Secluded experimental Mojave Area",
92                 "value"    =>    "a3",
93             ),
94         );
95         
96        /**
97         * method for retrieving the values for the element, has to return them as
98         * key => value pairs in an array.
99         *
100         * @access    public
101         * @return    array    $values    The values for the element.
102         */
103         function getValues()
104         {
105             return $this->values;
106         }
107     }
108     
109
110     // get the element
111     $el =& $form->getElementByName( 'area' );
112     
113     // instantiate the example data source
114     $datasrcObject    =    new exampleDatasrc;
115     
116     // tell the element to use the datasource object
117     $el->setDataSource( $datasrcObject );
118
119
120     // PROCESS FORM ------------------------------------------------------
121
122     // create the needed renderer
123     $renderer    =&    patForms::createRenderer( "Array" );
124     
125     // set the renderer
126     $form->setRenderer( $renderer );
127     
128     // use auto-validation
129     $form->setAutoValidate( 'save' );
130
131     // serialize the elements
132     $elements    =    $form->renderForm();
133     
134     
135     // ERROR DISPLAY ------------------------------------------------------
136     // ask the form if it has been submitted and display errors. For
137     // convenience and also to keep the examples easy to understand, all
138     // the following examples will use teh helper methods of the examples
139     // framework to display the errors and the form.
140     if( $form->isSubmitted() )
141     {
142         displayErrors( $form ); // see patExampleGen/customFunctions.php
143     }
144
145     // DISPLAY FORM ------------------------------------------------------
146     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
147
148
149     
150     
151     // EXAMPLE END ------------------------------------------------------
152     $exampleGen->displayFooter();
153 ?>
Note: See TracBrowser for help on using the browser.