root/tags/RELEASE_0_9_0B1/examples/example_api_factory_detailed.php

Revision 117, 4.0 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  * Example that shows the usage of the patForms factory calls
4  * and its api to handle a form's elements. Detailed version
5  * which shows the extended methods and manual creation of
6  * elements.
7  *
8  * $Id$
9  *
10  * @access        public
11  * @package        patForms
12  * @subpackage    Examples
13  * @author        Sebastian Mordziol <argh@php-tools.net>
14  * @license        LGPL, see license.txt for details
15  * @link        http://www.php-tools.net
16  */
17
18     /**
19      * Main examples prepend file, needed *only* for the examples framework!
20      */
21     include_once 'patExampleGen/prepend.php';
22     $exampleGen->displayHead( 'Example' );
23
24     
25     // EXAMPLE START ------------------------------------------------------
26
27     /**
28      * main patForms class
29      */
30     require_once $neededFiles['patForms'];
31     
32     /**
33      * patErrorManager class
34      */
35     require_once $neededFiles['patErrorManager'];
36
37
38     // username element attribute collection
39     $username    =    array(
40             "required"        =>    "yes",
41             "display"        =>    "yes",
42             "edit"            =>    "yes",
43             "label"            =>    "Username",
44             "description"    =>    "Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]",
45             "default"        =>    "argh",
46             "maxlength"        =>    "15",
47             "minlength"        =>    "4",
48             "title"            =>    "Username",
49     );
50     
51     // password element attribute collection
52     $password    =    array(
53             "type"            =>    "password",
54             "required"        =>    "yes",
55             "display"        =>    "yes",
56             "edit"            =>    "yes",
57             "label"            =>    "Password",
58             "description"    =>    "Enter your password here. Maximum length: [ELEMENT_MAXLENGTH]",
59             "maxlength"        =>    "15",
60             "minlength"        =>    "4",
61             "title"            =>    "Password",
62     );
63
64     // create the form - static function call, creates a new
65     // patForms object.
66     $form    =&    patForms::createForm();
67     
68     // set the form's name (needed to generate the form tag itself).
69     $form->setAttribute( 'name', 'myForm' );
70
71     // create the needed elements via the patForms factory
72     // using the attribute collections above
73     $el1 = $form->createElement( "user", "String", $username );
74     $el2 = $form->createElement( "pass", "String", $password );
75     
76     // now we can add our element objects to the form
77     $form->addElement( $el1 );
78     $form->addElement( $el2 );
79
80     // create the needed renderer so patForms knows how to
81     // display the elements. In this case the array renderer
82     // is used, wich returns all serialized elements in an array.
83     $renderer    =&    patForms::createRenderer( "Array" );
84     
85     // give patForms the array renderer
86     $form->setRenderer( $renderer );
87     
88     // serialize the elements using the array renderer - in this
89     // array are contained all serialized elements along with their
90     // attribute collection for easy access. You decide where and
91     // what to display.
92     $elements = $form->renderForm();
93     
94     
95     // ERROR DISPLAY ------------------------------------------------------
96
97     // check if form has been submitted
98     if( isset( $_POST['save'] ) )
99     {
100         // tell the form it has been submitted
101         $form->setSubmitted( true );
102         
103         // validate the form - if the validation fails, this returns
104         // false so it is easy to check.
105         if( !$form->validateForm() )
106         {
107             // retrieve the validation errors
108             $errors = $form->getValidationErrors();
109
110             echo "<b>patForms:</b> validation failed. Errors:<br><br>";
111             
112             // each element can have several error messages.
113             foreach( $errors as $elementName => $elementErrors )
114             {
115                 if( empty( $elementErrors ) )
116                     continue;
117                     
118                 echo 'Field: <b>'.$elementName.'</b>';
119                 echo "<pre>";
120                 print_r( $elementErrors );
121                 echo "</pre>";
122             }
123         }
124         else
125         {
126             echo "<b>patForms:</b> validation successful.<br><br>";
127         }
128     }
129
130
131     // DISPLAY FORM ------------------------------------------------------
132
133     // output the opening form tag
134     echo     $form->serializeStart();
135
136     // display all elements
137     foreach( $elements as $element )
138     {
139         echo $element["label"]."<br>";
140         echo "<div>".$element["element"]."</div>";
141         echo "<i>".$element["description"]."</i><br><br>";
142     }
143
144     // submit button, closing form tag
145     echo '<input type="submit" name="save" value="Save form"/><br><br>';
146     echo $form->serializeEnd();
147
148
149     
150     
151     // EXAMPLE END ------------------------------------------------------
152     $exampleGen->displayFooter();
153 ?>
154
Note: See TracBrowser for help on using the browser.