root/tags/RELEASE_0_9_0B1/examples/example_api_default_attributes.php

Revision 117, 2.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  * Example showing how to set default attributes for all elements in a form.
4  *
5  * $Id$
6  *
7  * @access        public
8  * @package        patForms
9  * @subpackage    Examples
10  * @author        Sebastian Mordziol <argh@php-tools.net>
11  * @license        LGPL, see license.txt for details
12  * @link        http://www.php-tools.net
13  */
14
15     /**
16      * Main examples prepend file, needed *only* for the examples framework!
17      */
18     include_once 'patExampleGen/prepend.php';
19     $exampleGen->displayHead( 'Example' );
20
21     
22     // EXAMPLE START ------------------------------------------------------
23
24     /**
25      * main patForms class
26      */
27     require_once $neededFiles['patForms'];
28     
29     /**
30      * patErrorManager class
31      */
32     require_once $neededFiles['patErrorManager'];
33
34     
35     // element definitions for this example
36     $elementsDefinition = array(
37         'username' => array(
38             'type' => 'String',
39             'attributes' => array(
40                 'required'        =>    'yes',
41                 'label'            =>    'Username',
42                 'description'    =>    'Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]',
43                 'title'            =>    'Username',
44             ),
45         ),
46         'password' => array(
47             'type' => 'String',
48             'attributes' => array(
49                 'required'        =>    'yes',
50                 'label'            =>    'Password',
51                 'type'            =>    'password',
52                 'description'    =>    'Enter your password here. Maximum length: [ELEMENT_MAXLENGTH]',
53                 'title'            =>    'Password',
54             )
55         ),
56     );
57     
58     // default attributes we want to use
59     $defaultAttributes = array(
60         'style'        =>    'width:100%',
61         'maxlength'    =>    '15',
62         'minlength'    =>    '4',
63     );
64     
65     // There are two ways to set the default attributes:
66     //
67     // 1. As a static function call (like we do here) before we create the form.
68     // 2. As a normal function call after creating the form.
69     //
70     // Here we have to use the static function call before creating the form, as
71     // we use the automatic element creation feature via the createForm() method,
72     // and the default attributes have to be set before any element is created.
73     patForms::setDefaultAttributes( $defaultAttributes );
74     
75     // create the form
76     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
77     
78     // use auto-validation
79     $form->setAutoValidate( 'save' );
80
81     // create the needed renderer
82     $renderer    =&    patForms::createRenderer( "Array" );
83     
84     // set the renderer
85     $form->setRenderer( $renderer );
86     
87     // serialize the elements using the array renderer
88     $elements    =    $form->renderForm();
89     
90     
91     // ERROR DISPLAY ------------------------------------------------------
92     // ask the form if it has been submitted and display errors
93     if( $form->isSubmitted() )
94     {
95         displayErrors( $form ); // see patExampleGen/customFunctions.php
96     }
97
98     // DISPLAY FORM ------------------------------------------------------
99     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
100
101     
102
103     // example footer
104     $exampleGen->displayFooter();
105 ?>
106
Note: See TracBrowser for help on using the browser.