* @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( 'callme' => array( 'type' => 'Switch', 'attributes' => array( 'required' => 'no', 'display' => 'yes', 'edit' => 'yes', 'label' => 'Call me', 'title' => 'Call me', 'description' => 'Select this if you want us to call you', 'value' => 'yes', ), ), 'phone' => array( 'type' => 'String', 'attributes' => array( 'required' => 'no', 'display' => 'yes', 'edit' => 'yes', 'id' => 'phone', 'label' => 'Phone number', 'title' => 'Phone number', 'description' => 'Your phone number', ), ), ); // 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' ); // create the rule - in this case, we use the ConditionalRequired // rule that allows setting fields in the form as required depending // on the value of another field. $condition =& patForms::createRule( 'ConditionalRequired' ); // set the condition field name, and the value the field has to be // set to for the rule to work. In this case, the user has to have // checked the 'Call me' checkbox. $condition->addCondition( 'callme', 'yes' ); // now set which fields should be set as required. $condition->setRequiredFields( array( 'phone' ) ); // add the rule to the form. In this case, we want the rule to be // executed before the actual form validation, as we need the fields' // required attribute to be set before we validate :) $form->addRule( $condition, PATFORMS_RULE_BEFORE_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(); ?>