root/tags/RELEASE_0_9_0B1/examples/example_api_setvalues.php

Revision 192, 4.9 kB (checked in by schst, 4 years ago)

fixed bug in setValues() that caused a fatal error when setting the value of a element that appeared multiple times

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