root/branches/gettext/examples/example_element_group.php

Revision 394, 5.6 kB (checked in by gerd, 1 year ago)

Switched to patI18n

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