root/tags/RELEASE_0_9_0B1/examples/example_datasrc_function.php

Revision 267, 3.1 kB (checked in by schst, 3 years ago)

Pass the element to the datasource, to allow context-sensitive datasources (as requested #151)

  • 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    /**
59     * The dummy function, used as a datasource for a patForms_Element_Enum
60     * object
61     *
62     * @access    public
63     * @param    string    $elementName    Name of the element
64     * @return    array    $areas            Associative array, keys are values, values are labels
65     */
66     function getAreas( $element )
67     {
68         return    array(
69             array(   
70                 'label' => 'No '.$element->getName(),
71                 'value'    =>    '1',
72             ),
73             array(   
74                 'label' => '----------',
75                 'value'    =>    '',
76             ),
77             array(   
78                 'label' => 'Foo',
79                 'value'    =>    '2',
80             ),
81             array(   
82                 'label' => 'Bar',
83                 'value'    =>    '3',
84             ),
85         );
86     }
87
88     // get the element
89     $el =& $form->getElementByName( 'area' );
90     
91     // set the data source for the enum element. Make sure that you always give
92     // the setDataSource() method a variable, as it is passed by reference and
93     // just strings will not work.
94     $datasourceFunction    =    "getAreas";
95
96     // tell the element to use the datasource function
97     $el->setDataSource( $datasourceFunction );
98
99
100     // PROCESS FORM ------------------------------------------------------
101
102     // create the needed renderer
103     $renderer    =&    patForms::createRenderer( "Array" );
104     
105     // set the renderer
106     $form->setRenderer( $renderer );
107     
108     // use auto-validation
109     $form->setAutoValidate( 'save' );
110
111     // serialize the elements
112     $elements    =    $form->renderForm();
113     
114     
115     // ERROR DISPLAY ------------------------------------------------------
116     // ask the form if it has been submitted and display errors. For
117     // convenience and also to keep the examples easy to understand, all
118     // the following examples will use teh helper methods of the examples
119     // framework to display the errors and the form.
120     if( $form->isSubmitted() )
121     {
122         displayErrors( $form ); // see patExampleGen/customFunctions.php
123     }
124
125     // DISPLAY FORM ------------------------------------------------------
126     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
127     
128     // EXAMPLE END ------------------------------------------------------
129     $exampleGen->displayFooter();
130 ?>
Note: See TracBrowser for help on using the browser.