root/tags/RELEASE_0_9_0B1/examples/example_clientside_javascript.php

Revision 117, 4.7 kB (checked in by argh, 4 years ago)

Massive update of the examples collection; added the patExampleGen examples framework; commented all examples; reworked all the templates...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms main example file that shows the usage of the patForms factory calls
4  * and its api to handle a form's elements.
5  *
6  * $Id$
7  *
8  * @access        public
9  * @package        patForms
10  * @subpackage    Examples
11  * @author        Sebastian Mordziol <argh@php-tools.net>
12  * @license        LGPL, see license.txt for details
13  * @link        http://www.php-tools.net
14  */
15 ?>
16 <html>
17 <head>
18     <title>formValidator example</title>
19 <style>
20     @import url( _styles.css );
21 </style>
22 </head>
23 <body>
24
25 <form name="myForm" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="get">
26
27 <?php
28
29     /**
30      * main patForms class
31      */
32     require_once $neededFiles['patForms'];
33     
34     /**
35      * patErrorManager class
36      */
37     require_once $neededFiles['patErrorManager'];
38
39     /**
40      * Helper functions needed by the patForms example files
41      */
42     include_once( "_helperfunctions.php" );
43
44     
45     
46     // test element definitions
47     $username    =    array(
48             "required"        =>    "yes",
49             "display"        =>    "yes",
50             "edit"            =>    "no",
51             "label"            =>    "Username",
52             "description"    =>    "Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]",
53             "position"        =>    "1",
54             "accesskey"        =>    "A",
55             "default"        =>    "argh",
56             "maxlength"        =>    "15",
57             "minlength"        =>    "4",
58             "title"            =>    "Username",
59             "onchange"        =>    "alert( pfe_username.getValue() );"
60     );
61     
62     $email    =    array(
63             "required"        =>    "yes",
64             "display"        =>    "yes",
65             "edit"            =>    "yes",
66             "label"            =>    "Email",
67             "description"    =>    "Enter your email address here.",
68             "position"        =>    "1",
69             "accesskey"        =>    "A",
70             "default"        =>    "argh@php-tools.net",
71             "maxlength"        =>    "50",
72             "minlength"        =>    "4",
73             "format"        =>    "email",
74             "title"            =>    "Email",
75             "onchange"        =>    "alert( pfe_email.getValue() );"
76     );
77
78     // create the attributes collection for the element
79     $pub    =    array(
80         'name'            =>    'publisher',
81         'required'        =>    'yes',
82         'display'        =>    'yes',
83         'edit'            =>    'yes',
84         'label'            =>    'Favorite publisher',
85         'description'    =>    'Choose your favorite comics publisher',
86         'position'        =>    '1',
87         'accesskey'        =>    'p',
88         'values'        =>    array(
89                                     array(
90                                             'label'    =>    'Please choose...',
91                                             'value'    =>    ''
92                                         ),
93                                     array(
94                                             'label'    =>    'DC Comics',
95                                             'value'    =>    'dc'
96                                         ),
97                                     array(
98                                             'label'    =>    'Marvel Comics',
99                                             'value'    =>    'marvel'
100                                         )
101                                 ),
102         "onchange"        =>    "alert( pfe_publisher.getValue() );"
103     );
104
105     
106     // create the form - static function call
107     $form    =    patForms::createForm();
108     
109     // you can set default attributes
110     $form->setDefaultAttributes( array(    "style" => "width:100%;" ) );
111
112     $form->setAttribute( 'name', 'myForm' );
113     
114     // set the mode of the form
115     //$form->setMode( "readonly" );
116     
117     // create needed elements
118     $el1    =    $form->createElement( "user", "String", $username );
119     $el2    =    $form->createElement( "email", "String", $email );
120     $el3    =    $form->createElement( 'publisher', 'Enum', $pub );
121     
122     // add elements to the form as needed
123     $form->addElement( $el1 );
124     $form->addElement( $el2 );
125     $form->addElement( $el3 );
126
127     // check if form has been saved
128     if( isset( $_GET["save"] ) )
129     {
130         // tell the element it has been submitted
131         $form->setSubmitted( true );
132
133         // validate the element
134         if( !$form->validateForm() )
135         {
136             echo "<b>patForms:</b> validation failed. Errors:<br><br>";
137             
138             $errors    =     $form->getValidationErrors();
139             
140             foreach( $errors as $elementName => $elementErrors )
141             {
142                 if( empty( $elementErrors ) )
143                     continue;
144                     
145                 echo 'Field: <b>'.$elementName.'</b>';
146                 echo "<pre>";
147                 print_r( $elementErrors );
148                 echo "</pre>";
149             }
150         }
151         else
152             echo "<b>patForms:</b> validation successful.<br><br>";
153     }
154
155     // create the needed renderer
156     $renderer    =&    patForms::createRenderer( "Array" );
157     
158     // set the renderer
159     $form->setRenderer( $renderer );
160     
161     // serialize the elements using the array renderer
162     $elements    =    $form->renderForm();
163
164     echo    $form->getScripts();
165     
166 ?>
167
168
169 <!-- DISPLAY SERIALIZED ELEMENTS ------------------------------------------------------ -->
170 <fieldset><legend><b>Generated elements</b></legend>
171 <?php
172     // output the elements
173     foreach( $elements as $element )
174     {
175         echo $element["label"]."<br>";
176         echo "<div>".$element["element"]."</div>";
177         echo "<i>".$element["description"]."</i><br><br>";
178     }
179 ?>
180 </fieldset><br>
181
182 <input type="submit" name="save" value="Save form"/><br><br>
183
184 <fieldset><legend><b>Javascript output</b></legend>
185 <pre>
186 <?php
187     echo    htmlspecialchars( $form->getScripts() );
188 ?>
189 </pre>
190 </fieldset><br>
191
192 <!-- SOURCE CODE OUTPUT -------------------------------------------------------------- -->
193 <fieldset><legend><b>Elements source code</b></legend>
194 <?php
195
196     // display the source of the elements
197     foreach( $elements as $element )
198     {
199         displaySource( $element['element'] );
200         echo '<br>';
201     }
202 ?>
203 </fieldset><br>
204
205 </form>
206 </body>
207 </html>
208
Note: See TracBrowser for help on using the browser.