root/trunk/examples/example_parser_simple.php

Revision 117, 2.4 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 for the Date element
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      * patForms parser class
31      */
32     require_once $neededFiles['patForms_Parser'];
33
34     /**
35      * patErrorManager class
36      */
37     require_once $neededFiles['patErrorManager'];
38
39         
40     // as we do not create a parser object and just call the parser's
41     // static methods, any needed options have to be called statically
42     // too, like setting the namespace or the cache folder to use:
43     patForms_Parser::setNamespace( "patForms" );
44     patForms_Parser::setCacheDir( "cache" );
45
46     // this method creates a complete patForms object in just one call,
47     // by setting the renderer to use, the source form template file,
48     // and the optional target template file.
49     $form =& patForms_Parser::createFormFromTemplate(
50         'SimpleRenderer',
51         'templates/example_parser_simple.fhtml',
52         'templates/example_parser_simple.html'
53     );
54
55     // use auto-validation
56     $form->setAutoValidate( 'save' );
57
58     // render the content into a variable, we will display it after
59     // the validation output
60     $content = $form->renderForm();
61
62     // check the form and display error messages if any
63     if( $form->isSubmitted() )
64     {
65         // get and display errors as needed. If no errors are found,
66         // this will return false so it is easy to check.
67         $errors = $form->getValidationErrors();
68         if( $errors )
69         {
70             echo "<b>patForms:</b> validation failed. Errors:<br><br>";
71             
72             // each element can have several error messages.
73             foreach( $errors as $elementName => $elementErrors )
74             {
75                 if( empty( $elementErrors ) )
76                     continue;
77                     
78                 echo 'Field: <b>'.$elementName.'</b>';
79                 echo "<pre>";
80                 print_r( $elementErrors );
81                 echo "</pre>";
82             }
83         }
84         else
85         {
86             echo "<b>patForms:</b> validation successful.<br><br>";
87         }
88     }
89
90     // now display the form
91     echo $content;
92     
93     
94     // EXAMPLE END ------------------------------------------------------
95     $exampleGen->displayFooter();
96 ?>
Note: See TracBrowser for help on using the browser.