root/trunk/examples/example_api_factory_simple.php

Revision 117, 3.3 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. 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     // serialize the elements using the array renderer - in this
87     // array are contained all serialized elements along with their
88     // attribute collection for easy access. You decide where and
89     // what to display.
90     $elements = $form->renderForm();
91     
92     if( isset( $_POST['save'] ) )
93     {
94         $form->setSubmitted( true );
95         
96         $form->validateForm();
97     }
98     
99     
100     // ERROR DISPLAY ------------------------------------------------------
101     // ask the form if it has been submitted and display errors. For
102     // convenience and also to keep the examples easy to understand, all
103     // the following examples will use teh helper methods of the examples
104     // framework to display the errors and the form.
105     if( $form->isSubmitted() )
106     {
107         displayErrors( $form ); // see patExampleGen/customFunctions.php
108     }
109
110     // DISPLAY FORM ------------------------------------------------------
111     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
112
113     
114     
115     
116     // EXAMPLE END ------------------------------------------------------
117     $exampleGen->displayFooter();
118 ?>
119
Note: See TracBrowser for help on using the browser.