* @license LGPL, see license.txt for details * @link http://www.php-tools.net */ /** * Main examples prepend file, needed *only* for the examples framework! */ include_once 'patExampleGen/prepend.php'; $exampleGen->displayHead( 'Example' ); // EXAMPLE START ------------------------------------------------------ /** * main patForms class */ require_once $neededFiles['patForms']; /** * patErrorManager class */ require_once $neededFiles['patErrorManager']; /** * localisation stuff */ require_once $neededFiles['patI18n_configure']; // element definitions for this example $elementsDefinition = array( 'publisher' => array( 'type' => 'Enum', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => 'Favorite publisher', 'description' => 'Choose your favorite comics publisher', 'values' => array( array( 'label' => 'Please choose...', 'value' => '' ), array( 'label' => 'DC Comics', 'value' => 'dc' ), array( 'label' => 'Marvel Comics', 'value' => 'marvel' ) ), ), ), 'hero' => array( 'type' => 'Enum', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => 'Favorite superhero', 'description' => 'Choose your favorite comics hero of the selected publisher', 'values' => array( array( 'label' => 'Please choose...', 'value' => '' ), array( 'label' => 'Superman', 'value' => 'superman' ), array( 'label' => 'The Flash', 'value' => 'flash' ), array( 'label' => 'Spider-Man', 'value' => 'spiderman' ), array( 'label' => 'The Orphan', 'value' => 'orphan' ), ), ), ), 'sidekick' => array( 'type' => 'Enum', 'attributes' => array( 'required' => 'no', 'display' => 'yes', 'edit' => 'yes', 'label' => 'Favorite Sidekick', 'description' => 'Choose your favorite sidekick of the selected hero', 'values' => array( array( 'label' => 'Please choose...', 'value' => '' ), array( 'label' => 'Krypto', 'value' => 'krypto' ), array( 'label' => 'Supergirl', 'value' => 'supergirl' ), array( 'label' => 'Kid Flash', 'value' => 'kidflash' ), array( 'label' => 'Mary-Jane', 'value' => 'maryjane' ), ), ), ), ); // create the form $form =& patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) ); // create the needed renderer $renderer =& patForms::createRenderer( "Array" ); // set the renderer $form->setRenderer( $renderer ); // use auto-validation $form->setAutoValidate( 'save' ); // [using javascript] // // You may enable client side validation using the enableOption() method. // As parameters you may pass the path to the script files, if the can be // included by the webserver, you should set the 'jsInclude' paarmeter to // 'true'. Otherwise the file contents will be included on the server and // echoed to the browser. // $jsOptions = array( 'folder' => '../patForms/Scripts', 'jInclude' => true ); $form->enableOption('scripts', $jsOptions); // enable client side scripts /* $form->enableOption('scripts'); */ // [small overview] // // what we are trying to achieve: have several lists of values that // have dependencies amongst each other - meaning that if you select // an entry in the first list, the second list is truncated so only // the entries corresponding to that choice are kept. // // as you can easily have another list depend on the values of the // second list as well, this can be nested as far as you like by simply // adding more rules. // create the rule for the first list. Note that we give the rule // an id; this is needed for the clientside functionality. $condition1 =& patForms::createRule( 'ConditionalEnum', 'pubhero' ); // set the source field $condition1->setSourceField( 'publisher' ); // set the target field (whose values depend on the source field) $condition1->setTargetField( 'hero' ); // if nothing is selected, no values are allowed in the target field $condition1->addCondition( '', array() ); // if one of the values is selected, the following lists are allowed: $condition1->addCondition( 'dc', array( '', 'superman', 'flash' ) ); $condition1->addCondition( 'marvel', array( '', 'spiderman', 'orphan' ) ); // create the second rule, which defines what values are allowed in the // third field depending on the values in the second. $condition2 =& patForms::createRule( 'ConditionalEnum', 'herokick' ); $condition2->setSourceField( 'hero' ); $condition2->setTargetField( 'sidekick' ); $condition2->addCondition( '', array() ); $condition2->addCondition( 'superman', array( '', 'krypto', 'supergirl' ) ); $condition2->addCondition( 'flash', array( '', 'kidflash' ) ); $condition2->addCondition( 'spiderman', array( '', 'maryjane' ) ); $condition2->addCondition( 'orphan', array( '' ) ); // add the rules to the form $form->addRule( $condition1, PATFORMS_RULE_AFTER_VALIDATION ); $form->addRule( $condition2, PATFORMS_RULE_AFTER_VALIDATION ); // serialize the elements $elements = $form->renderForm(); // ERROR DISPLAY ------------------------------------------------------ // ask the form if it has been submitted and display errors. For // convenience and also to keep the examples easy to understand, all // the following examples will use teh helper methods of the examples // framework to display the errors and the form. if( $form->isSubmitted() ) { displayErrors( $form ); // see patExampleGen/customFunctions.php } // DISPLAY FORM ------------------------------------------------------ displayForm( $form, $elements ); // see patExampleGen/customFunctions.php // EXAMPLE END ------------------------------------------------------ $exampleGen->displayFooter(); ?>