root/tags/RELEASE_0_9_0B1/examples/example_api_autovalidate.php

Revision 117, 2.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 showing the autovalidation feature.
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                 'maxlength'        =>    '15',
44                 'minlength'        =>    '4',
45                 'title'            =>    'Username',
46             ),
47         ),
48     );
49     
50     // create the form
51     $form    =&    patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) );
52     
53     // create the needed renderer
54     $renderer    =&    patForms::createRenderer( "Array" );
55     
56     // set the renderer
57     $form->setRenderer( $renderer );
58     
59     // use auto-validation - in this case, the form is validated
60     // automatically when renderForm() is called, and you can
61     // check whether the form is valid by checking for validation
62     // errors via the getValidationErrors() method.
63     $form->setAutoValidate( 'save' );
64
65     // serialize the elements
66     $elements = $form->renderForm();
67     
68     
69     // ERROR DISPLAY ------------------------------------------------------
70     // ask the form if it has been submitted. Note that you
71     // do not need the setSubmitted() and validateForm() function
72     // calls with the autovalidation feature. You just need to
73     // make sure you call this after the renderForm() call, at which
74     // point the form is automatically validated.
75     if( $form->isSubmitted() )
76     {
77         displayErrors( $form ); // see patExampleGen/customFunctions.php
78     }
79
80     // DISPLAY FORM ------------------------------------------------------
81     displayForm( $form, $elements ); // see patExampleGen/customFunctions.php
82
83
84     
85     
86     // EXAMPLE END ------------------------------------------------------
87     $exampleGen->displayFooter();
88 ?>
89
Note: See TracBrowser for help on using the browser.