root/branches/gettext/examples/example_api_setvalues.php

Revision 394, 5.0 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 that shows the usage of patForms::setValues()
4  *
5  * $Id$
6  *
7  * @access        public
8  * @package        patForms
9  * @subpackage    Examples
10  * @author        Stephan Schmidt <schst@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     // element definitions for this example - note
41     // that they are all grouped together in one
42     // array, with the additional keys 'type' and
43     // 'attributes'.
44     $elementsDefinition = array(
45         'username' => array(
46             'type' => 'String',
47             'attributes' => array(
48                 'required'        =>    'yes',
49                 'display'        =>    'yes',
50                 'edit'            =>    'yes',
51                 'label'            =>    'Username',
52                 'description'    =>    'Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]',
53                 'default'        =>    'argh',
54                 'maxlength'        =>    '15',
55                 'minlength'        =>    '4',
56                 'title'            =>    'Username',
57             ),
58         ),
59         'password' => array(
60             'type' => 'String',
61             'attributes' => array(
62                 'type'            =>    'password',
63                 'required'        =>    'yes',
64                 'display'        =>    'yes',
65                 'edit'            =>    'yes',
66                 'label'            =>    'Password',
67                 'description'    =>    'Enter your password here. Maximum length: [ELEMENT_MAXLENGTH]',
68                 'maxlength'        =>    '15',
69                 'minlength'        =>    '4',
70                 'title'            =>    'Password',
71             ),
72         ),
73     );
74
75     // create the form - and just set the elements definition
76     // directly here to have patForms automatically create and
77     // add all needed elements. The attributes of the form itself
78     // can also be set here directly (the name, for instance)
79     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
80     
81     // we are adding two radio elements that use the same name
82     // so only one of them can be checked at once.
83     $radioAtts1 = array(
84                         'required'        =>    'yes',
85                         'display'        =>    'yes',
86                         'edit'            =>    'yes',
87                         'label'            =>    'Area 51',
88                         'title'            =>    'Area 51',
89                         'description'    =>    'Click to select the area &quot;[ELEMENT_LABEL]&quot;.',
90                         'clicklabel'    =>    'yes',
91                         'value'            =>    '51',
92                     );
93     $radioAtts2 = array(
94                         'required'        =>    'yes',
95                         'display'        =>    'yes',
96                         'edit'            =>    'yes',
97                         'label'            =>    'Area 52',
98                         'title'            =>    'Area 52',
99                         'description'    =>    'Click to select the area &quot;[ELEMENT_LABEL]&quot;.',
100                         'clicklabel'    =>    'yes',
101                         'value'            =>    '52',
102                     );
103     
104     // create the elements
105     $radio1 = &$form->createElement('area', 'Radio', $radioAtts1);
106     $radio2 = &$form->createElement('area', 'Radio', $radioAtts2);
107
108     // add them to the form
109     $form->addElement($radio1);
110     $form->addElement($radio2);
111     
112     // create the needed renderer so patForms knows how to
113     // display the elements. In this case the array renderer
114     // is used, wich returns all serialized elements in an array.
115     $renderer    =&    patForms::createRenderer( "Array" );
116     
117     // set the values for the elements
118     // The keys of the array must match the names of the elements.
119     // If a key does not have a corresponding element, it will be skipped.
120     //
121     // If you set the second parameter to true, it will override the user input
122     // and allow you to build a form with static values
123     $form->setValues(
124                        array(
125                                'username' => 'schst',
126                                'password' => 'secret',
127                                'area'     => 52,
128                                'bogus'    => 'Not an element of the form'
129                            ),
130                        false
131                    );
132     
133     // give patForms the array renderer
134     $form->setRenderer( $renderer );
135     
136     // serialize the elements using the array renderer - in this
137     // array are contained all serialized elements along with their
138     // attribute collection for easy access. You decide where and
139     // what to display.
140     $elements = $form->renderForm();
141     
142     if( isset( $_POST['save'] ) )
143     {
144         $form->setSubmitted( true );
145         
146         $form->validateForm();
147     }
148     
149     
150     // ERROR DISPLAY ------------------------------------------------------
151     // ask the form if it has been submitted and display errors. For
152     // convenience and also to keep the examples easy to understand, all
153     // the following examples will use teh helper methods of the examples
154     // framework to display the errors and the form.
155     if( $form->isSubmitted() )
156     {
157         displayErrors( $form ); // see patExampleGen/customFunctions.php
158     }
159
160     // DISPLAY FORM ------------------------------------------------------
161     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
162
163     
164     
165     
166     // EXAMPLE END ------------------------------------------------------
167     $exampleGen->displayFooter();
168 ?>
169
Note: See TracBrowser for help on using the browser.