root/trunk/examples/example_element_group.php

Revision 168, 5.5 kB (checked in by schst, 4 years ago)

getElementByName() now searches in containers like groups

  • 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     // normal elements definition - these will be added
36     // to the form as usual.
37     $elementsDefinition = array(
38         'username' => array(
39             'type' => 'String',
40             'attributes' => array(
41                 'required'        =>    'yes',
42                 'display'        =>    'yes',
43                 'edit'            =>    'yes',
44                 'label'            =>    'Username',
45                 'description'    =>    'Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]',
46                 'default'        =>    'argh',
47                 'maxlength'        =>    '15',
48                 'minlength'        =>    '4',
49                 'title'            =>    'Username',
50             ),
51         ),
52         'password' => array(
53             'type' => 'String',
54             'attributes' => array(
55                 "type"            =>    "password",
56                 "required"        =>    "yes",
57                 "display"        =>    "yes",
58                 "edit"            =>    "yes",
59                 "label"            =>    "Password",
60                 "description"    =>    "Enter your password here. Maximum length: [ELEMENT_MAXLENGTH]",
61                 "maxlength"        =>    "15",
62                 "minlength"        =>    "4",
63                 "title"            =>    "Password",
64             ),
65         ),
66     );
67
68     // definition of the group element itself - it does not need
69     // that many attributes, as it is only a container element.
70     $groupDefinition = array(
71         'display'        =>    'yes',
72         'label'            =>    'Basic Data',
73         'title'            =>    'Basic Data',
74         'description'    =>    'Basic authentication data',
75     );
76
77     // definition of the area element which will be a part of the group
78     $areaDefinition = array(
79         'required'        =>    'yes',
80         'display'        =>    'yes',
81         'edit'            =>    'yes',
82         'label'            =>    'Area',
83         'title'            =>    'Area',
84         'description'    =>    'Choose the area to access here.',
85         'values'        =>    array(
86             array(
87                 'label'    =>    'Please choose an area...',
88                 'value'    =>    '',
89             ),
90             array(
91                 'label'    =>    'Very pretty area',
92                 'value'    =>    'a01',
93             ),
94             array(
95                 'label'    =>    'No-nonsense area',
96                 'value'    =>    'a02',
97             ),
98         ),
99     );
100     
101     // definition of the secure element which will be a part of the group
102     $secureDefinition = array(
103         'required'        =>    'yes',
104         'display'        =>    'yes',
105         'edit'            =>    'yes',
106         'label'            =>    'Secure access',
107         'title'            =>    'Secure access',
108         'description'    =>    'Choose the area to access here.',
109         'clicklabel'    =>    'yes',
110         'value'            =>    'yes',
111         'id'            =>    'mySecure'
112     );
113     
114     // create the form
115     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
116     
117     // create the needed renderer
118     $renderer    =&    patForms::createRenderer( "Array" );
119
120     // set the renderer
121     $form->setRenderer( $renderer );
122     
123     // create the group element
124     $groupEl =& patForms::createElement( 'loginOptions', 'Group', $groupDefinition );
125     
126     // give the group the needed renderer too - we can use
127     // the same here
128     $groupEl->setRenderer( $renderer );
129
130     // create the elements for the group
131     $gel1 =& patForms::createElement( 'area', 'Enum', $areaDefinition );
132     $gel2 =& patForms::createElement( 'secure', 'Switch', $secureDefinition );
133
134     // now add the elements to the group
135     $groupEl->addElement( $gel1 );
136     $groupEl->addElement( $gel2 );
137     
138     // and add the group to the form
139     $form->addElement( $groupEl );
140     
141     // use auto-validation
142     $form->setAutoValidate( 'save' );
143
144     /*
145     $foo = $form->getElementByName('area');
146     echo $foo->serialize();
147     
148     $bar = $form->getElementById('mySecure');
149     echo $bar->serialize();
150     */
151
152     // serialize the elements
153     $elements    =    $form->renderForm();
154     
155     
156     // ERROR DISPLAY ------------------------------------------------------
157     // ask the form if it has been submitted and display errors. For
158     // convenience and also to keep the examples easy to understand, all
159     // the following examples will use teh helper methods of the examples
160     // framework to display the errors and the form.
161     if( $form->isSubmitted() )
162     {
163         displayErrors( $form ); // see patExampleGen/customFunctions.php
164     }
165
166     // DISPLAY FORM ------------------------------------------------------
167     // output the opening form tag
168     echo $form->serializeStart();
169
170     // display all elements
171     foreach( $elements as $element )
172     {
173         echo '<div style="margin-bottom:8px;">';
174         
175         // a group acts like a form, so the group element's element
176         // is actually a new array with a list of elements that have
177         // been rendered each - we simply display them all too.
178         //
179         // NOTE: this is just to show you how the structure looks like!
180         if( is_array( $element['element'] ) )
181         {
182             echo '<fieldset><legend>'.$element['label'].'</legend>';
183             
184             foreach( $element['element'] as $subelement )
185             {
186                 echo '<div style="margin-bottom:8px;">';
187                 echo $subelement['label']."<br>";
188                 echo $subelement['element'].'<br/>';
189                 echo '<i>'.$subelement['description'].'</i></div>';
190             }
191             
192             echo '</fieldset>';
193         }
194         // normal element
195         else
196         {
197             echo $element['label']."<br>";
198             echo "<div>".$element["element"]."</div>";
199             if( isset( $element["description"] ) )
200             {
201                 echo "<i>".$element["description"]."</i><br>";
202             }
203         }
204             
205         echo '</div>';
206     }
207
208     // submit button, closing form tag
209     echo '<input type="submit" name="save" value="Save form"/><br><br>';
210     echo $form->serializeEnd();
211
212
213     
214     
215     // EXAMPLE END ------------------------------------------------------
216     $exampleGen->displayFooter();
217 ?>
Note: See TracBrowser for help on using the browser.