root/trunk/examples/example_api_replace_element.php

Revision 144, 4.0 kB (checked in by schst, 4 years ago)

added replace element

  • 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 the patForms factory calls
4  * and its api to handle a form's elements. Simplified version
5  * that used patForms' automation features
6  *
7  * $Id$
8  *
9  * @access        public
10  * @package        patForms
11  * @subpackage    Examples
12  * @author        Sebastian Mordziol <argh@php-tools.net>
13  * @license        LGPL, see license.txt for details
14  * @link        http://www.php-tools.net
15  */
16
17     /**
18      * Main examples prepend file, needed *only* for the examples framework!
19      */
20     include_once 'patExampleGen/prepend.php';
21     $exampleGen->displayHead( 'Example' );
22
23     
24     // EXAMPLE START ------------------------------------------------------
25
26     /**
27      * main patForms class
28      */
29     require_once $neededFiles['patForms'];
30     
31     /**
32      * patErrorManager class
33      */
34     require_once $neededFiles['patErrorManager'];
35
36     
37     // element definitions for this example - note
38     // that they are all grouped together in one
39     // array, with the additional keys 'type' and
40     // 'attributes'.
41     $elementsDefinition = array(
42         'username' => array(
43             'type' => 'String',
44             'attributes' => array(
45                 'required'        =>    'yes',
46                 'display'        =>    'yes',
47                 'edit'            =>    'yes',
48                 'label'            =>    'Username',
49                 'description'    =>    'Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]',
50                 'default'        =>    'argh',
51                 'maxlength'        =>    '15',
52                 'minlength'        =>    '4',
53                 'title'            =>    'Username',
54             ),
55         ),
56         'password' => array(
57             'type' => 'String',
58             'attributes' => array(
59                 'type'            =>    'password',
60                 'required'        =>    'yes',
61                 'display'        =>    'yes',
62                 'edit'            =>    'yes',
63                 'label'            =>    'Password',
64                 'description'    =>    'Enter your password here. Maximum length: [ELEMENT_MAXLENGTH]',
65                 'maxlength'        =>    '15',
66                 'minlength'        =>    '4',
67                 'title'            =>    'Password',
68             ),
69         ),
70     );
71
72     // create the form - and just set the elements definition
73     // directly here to have patForms automatically create and
74     // add all needed elements. The attributes of the form itself
75     // can also be set here directly (the name, for instance)
76     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
77     
78     // create the needed renderer so patForms knows how to
79     // display the elements. In this case the array renderer
80     // is used, wich returns all serialized elements in an array.
81     $renderer    =&    patForms::createRenderer( "Array" );
82     
83     // give patForms the array renderer
84     $form->setRenderer( $renderer );
85     
86     $attributes = array(
87                         'required'        =>    'yes',
88                         'display'        =>    'yes',
89                         'edit'            =>    'yes',
90                         'label'            =>    'Area',
91                         'title'            =>    'Area',
92                         'description'    =>    'Choose the area to access here.',
93                         'values'        =>    array(
94                             array(
95                                 'label'    =>    'Please choose an area...',
96                                 'value'    =>    '',
97                             ),
98                             array(
99                                 'label'    =>    'Very pretty area',
100                                 'value'    =>    'a01',
101                             ),
102                             array(
103                                 'label'    =>    'No-nonsense area',
104                                 'value'    =>    'a02',
105                             ),
106                         )
107                     );
108     $area = &patForms::createElement('area', 'Enum', $attributes);
109
110     $user = &$form->getElement('username');
111     $form->replaceElement($user, $area);
112     
113     $form->addElement($user);
114     
115     // serialize the elements using the array renderer - in this
116     // array are contained all serialized elements along with their
117     // attribute collection for easy access. You decide where and
118     // what to display.
119     $elements = $form->renderForm();
120     
121     if( isset( $_POST['save'] ) )
122     {
123         $form->setSubmitted( true );
124         
125         $form->validateForm();
126     }
127     
128     
129     // ERROR DISPLAY ------------------------------------------------------
130     // ask the form if it has been submitted and display errors. For
131     // convenience and also to keep the examples easy to understand, all
132     // the following examples will use teh helper methods of the examples
133     // framework to display the errors and the form.
134     if( $form->isSubmitted() )
135     {
136         displayErrors( $form ); // see patExampleGen/customFunctions.php
137     }
138
139     // DISPLAY FORM ------------------------------------------------------
140     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
141
142     
143     
144     
145     // EXAMPLE END ------------------------------------------------------
146     $exampleGen->displayFooter();
147 ?>
148
Note: See TracBrowser for help on using the browser.