root/branches/gettext/examples/example_api_factory_detailed.php

Revision 394, 4.1 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 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      * localisation stuff
39      */
40     require_once $neededFiles['patI18n_configure'];
41
42     // username element attribute collection
43     $username    =    array(
44             "required"        =>    "yes",
45             "display"        =>    "yes",
46             "edit"            =>    "yes",
47             "label"            =>    "Username",
48             "description"    =>    "Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]",
49             "default"        =>    "argh",
50             "maxlength"        =>    "15",
51             "minlength"        =>    "4",
52             "title"            =>    "Username",
53     );
54     
55     // password element attribute collection
56     $password    =    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     // create the form - static function call, creates a new
69     // patForms object.
70     $form    =&    patForms::createForm();
71     
72     // set the form's name (needed to generate the form tag itself).
73     $form->setAttribute( 'name', 'myForm' );
74
75     // create the needed elements via the patForms factory
76     // using the attribute collections above
77     $el1 = $form->createElement( "user", "String", $username );
78     $el2 = $form->createElement( "pass", "String", $password );
79     
80     // now we can add our element objects to the form
81     $form->addElement( $el1 );
82     $form->addElement( $el2 );
83
84     // create the needed renderer so patForms knows how to
85     // display the elements. In this case the array renderer
86     // is used, wich returns all serialized elements in an array.
87     $renderer    =&    patForms::createRenderer( "Array" );
88     
89     // give patForms the array renderer
90     $form->setRenderer( $renderer );
91     
92     // serialize the elements using the array renderer - in this
93     // array are contained all serialized elements along with their
94     // attribute collection for easy access. You decide where and
95     // what to display.
96     $elements = $form->renderForm();
97     
98     
99     // ERROR DISPLAY ------------------------------------------------------
100
101     // check if form has been submitted
102     if( isset( $_POST['save'] ) )
103     {
104         // tell the form it has been submitted
105         $form->setSubmitted( true );
106         
107         // validate the form - if the validation fails, this returns
108         // false so it is easy to check.
109         if( !$form->validateForm() )
110         {
111             // retrieve the validation errors
112             $errors = $form->getValidationErrors();
113
114             echo "<b>patForms:</b> validation failed. Errors:<br><br>";
115             
116             // each element can have several error messages.
117             foreach( $errors as $elementName => $elementErrors )
118             {
119                 if( empty( $elementErrors ) )
120                     continue;
121                     
122                 echo 'Field: <b>'.$elementName.'</b>';
123                 echo "<pre>";
124                 print_r( $elementErrors );
125                 echo "</pre>";
126             }
127         }
128         else
129         {
130             echo "<b>patForms:</b> validation successful.<br><br>";
131         }
132     }
133
134
135     // DISPLAY FORM ------------------------------------------------------
136
137     // output the opening form tag
138     echo     $form->serializeStart();
139
140     // display all elements
141     foreach( $elements as $element )
142     {
143         echo $element["label"]."<br>";
144         echo "<div>".$element["element"]."</div>";
145         echo "<i>".$element["description"]."</i><br><br>";
146     }
147
148     // submit button, closing form tag
149     echo '<input type="submit" name="save" value="Save form"/><br><br>';
150     echo $form->serializeEnd();
151
152
153     
154     
155     // EXAMPLE END ------------------------------------------------------
156     $exampleGen->displayFooter();
157 ?>
158
Note: See TracBrowser for help on using the browser.