Show
Ignore:
Timestamp:
08/26/04 15:08:37 (4 years ago)
Author:
argh
Message:

Massive update of the examples collection; added the patExampleGen examples framework; commented all examples; reworked all the templates...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/examples/example_element_enum.php

    r2 r117  
    11<?php 
    22/** 
    3  * patForms example for enum elements with a function as a datasource 
     3 * Example for the Date element 
    44 *  
    55 * $Id$ 
     
    1212 * @link        http://www.php-tools.net 
    1313 */ 
    14 ?> 
    15 <html> 
    16 <head> 
    17     <title>formValidator example</title> 
    18 <style> 
    19     @import url( _styles.css ); 
    20 </style> 
    21 </head> 
    22 <body marginheight="10" marginwidth="10" leftmargin="10" rightmargin="10" topmargin="10" bottommargin="10"> 
    23  
    24 <div class="head"><b>patForms Element:</b> Enum</div> 
    25  
    26 <form action="example_element_enum.php" method="get"> 
    27  
    28 <?php 
    29     error_reporting( E_ALL ); 
    3014 
    3115    /** 
    32      * Main patForms element class 
     16     * Main examples prepend file, needed *only* for the examples framework! 
    3317     */ 
    34     require_once '../patForms/Element.php'; 
     18    include_once 'patExampleGen/prepend.php'; 
     19    $exampleGen->displayHead( 'Example' ); 
     20 
    3521     
     22    // EXAMPLE START ------------------------------------------------------ 
     23 
    3624    /** 
    37      * Enum element class 
     25     * main patForms class 
    3826     */ 
    39     include_once( "../patForms/Element/Enum.php" )
    40  
     27    require_once $neededFiles['patForms']
     28     
    4129    /** 
    4230     * patErrorManager class 
    4331     */ 
    44     require_once 'pat/patErrorManager.php'
     32    require_once $neededFiles['patErrorManager']
    4533 
    46     /** 
    47      * Helper functions needed by the patForms example files 
    48      */ 
    49     include_once( "_helperfunctions.php" ); 
    50  
    51     // create the attribute collection for the enum field, including 
    52     // default values list in case something goes wrong 
    53     $attributes =   array( 
    54         'name'          =>  'area', 
    55         'required'      =>  'yes', 
    56         'style'         =>  'width:100%', 
    57         'display'       =>  'yes', 
    58         'edit'          =>  'yes', 
    59         'label'         =>  'Area', 
    60         'description'   =>  'Choose the area to access here.', 
    61         'position'      =>  '3', 
    62         'accesskey'     =>  'C', 
    63         'size'          =>  'auto', 
    64         'title'         =>  'Area', 
    65         'values'        =>  array( 
    66             array( 
    67                 'label' =>  'Please choose an area...', 
    68                 'value' =>  '', 
     34     
     35    // element definitions for this example 
     36    $elementsDefinition = array( 
     37        'username' => array( 
     38            'type' => 'String', 
     39            'attributes' => array( 
     40                'required'      =>  'yes', 
     41                'display'       =>  'yes', 
     42                'edit'          =>  'yes', 
     43                'label'         =>  'Username', 
     44                'title'         =>  'Username', 
     45                'description'   =>  'Enter your username here. Maximum length: [ELEMENT_MAXLENGTH]', 
     46                'default'       =>  'argh', 
     47                'maxlength'     =>  '15', 
     48                'minlength'     =>  '4', 
    6949            ), 
    70             array( 
    71                 'label' =>  'Very pretty area', 
    72                 'value' =>  'a01', 
    73             ), 
    74             array( 
    75                 'label' =>  'No-nonsense area', 
    76                 'value' =>  'a02', 
     50        ), 
     51        'area' => array( 
     52            'type' => 'Enum', 
     53            'attributes' => array( 
     54                'required'      =>  'yes', 
     55                'display'       =>  'yes', 
     56                'edit'          =>  'yes', 
     57                'label'         =>  'Area', 
     58                'title'         =>  'Area', 
     59                'description'   =>  'Choose the area to access here.', 
     60                'values'        =>  array( 
     61                    array( 
     62                        'label' =>  'Please choose an area...', 
     63                        'value' =>  '', 
     64                    ), 
     65                    array( 
     66                        'label' =>  'Very pretty area', 
     67                        'value' =>  'a01', 
     68                    ), 
     69                    array( 
     70                        'label' =>  'No-nonsense area', 
     71                        'value' =>  'a02', 
     72                    ), 
     73                ), 
    7774            ), 
    7875        ), 
    7976    ); 
     77     
     78    // create the form 
     79    $form   =&  patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) ); 
     80     
     81    // create the needed renderer 
     82    $renderer   =&  patForms::createRenderer( "Array" ); 
     83     
     84    // set the renderer 
     85    $form->setRenderer( $renderer ); 
     86     
     87    // use auto-validation 
     88    $form->setAutoValidate( 'save' ); 
    8089 
    81     // create the enum element 
    82     $el    =& new patForms_Element_Enum( 'html' ); 
     90    // serialize the elements 
     91    $elements  =   $form->renderForm(); 
    8392     
    84     // set the attribute collection to use 
    85     $el->setAttributes( $attributes ); 
    8693     
    87     // check if form has been saved 
    88     if( isset( $_GET["save"] ) ) 
     94    // ERROR DISPLAY ------------------------------------------------------ 
     95    // ask the form if it has been submitted and display errors. For 
     96    // convenience and also to keep the examples easy to understand, all 
     97    // the following examples will use teh helper methods of the examples 
     98    // framework to display the errors and the form. 
     99    if( $form->isSubmitted() ) 
    89100    { 
    90         // tell the element it has been submitted 
    91         $el->setSubmitted( true ); 
    92  
    93         // validate the element 
    94         if( !$el->validate() ) 
    95         { 
    96             echo "<b>patFormsElement Enum:</b> validation failed. Errors:<br>"; 
    97             echo "<pre>"; 
    98             print_r( $el->getValidationErrors() ); 
    99             echo "</pre>"; 
    100         } 
    101         else 
    102             echo "<b>patFormsElement Enum:</b> validation successful.<br><br>"; 
     101        displayErrors( $form ); // see patExampleGen/customFunctions.php 
    103102    } 
    104103 
    105     // retrieve the serialized content of the element 
    106     $html   =   $el->serialize(); 
     104    // DISPLAY FORM ------------------------------------------------------ 
     105    displayForm( $form, $elements ); // see patExampleGen/customFunctions.php 
     106 
     107 
     108     
     109     
     110    // EXAMPLE END ------------------------------------------------------ 
     111    $exampleGen->displayFooter(); 
    107112?> 
    108  
    109 <fieldset><legend><b>Generated element</b></legend> 
    110 <?php 
    111     // display the element 
    112     echo $attributes["label"]."<br>"; 
    113     echo "<div>".$html."</div>"; 
    114     echo "<i>".$attributes["description"]."</i><br><br>"; 
    115 ?> 
    116 </fieldset><br> 
    117  
    118 <input type="submit" name="save" value="Save element"/><br><br> 
    119  
    120 <fieldset><legend><b>Element source code</b></legend> 
    121     <?php displaySource( $html );  ?> 
    122 </fieldset> 
    123  
    124 </form> 
    125  
    126 </body> 
    127 </html>