* @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( 'username' => array( 'type' => 'String', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => 'Username', 'title' => 'Username', 'description' => 'Please enter your name here', ), ), ); // create the form $form =& patForms::createForm( $elementsDefinition, array( 'name' => 'myForm' ) ); // register the event handlers for the onError and onSuccess events - in // this case we have one to display any form errors automatically, and the // other to automatically store the submitted data once it has been validated. $form->registerEventHandler( 'onError', 'displayFormErrors' ); $form->registerEventHandler( 'onSuccess', 'displaySuccess' ); /** * Simple event handler that displays the errors. * * @access public * @param object patForms form object * @param string event name * @return mixed any value your function should return. You will get the value as a return value of patForms::renderForm() * @see patForms::registerEventHandler() */ function displayFormErrors(&$form, $event) { // get and display errors as needed $errors = $form->getValidationErrors(); if( $errors ) { echo "patForms: validation failed. Errors:

"; // each element can have several error messages. foreach( $errors as $elementName => $elementErrors ) { if( empty( $elementErrors ) ) continue; echo 'Field: '.$elementName.''; echo "
";
	            print_r( $elementErrors );
	            echo "
"; } } else { echo "patForms: validation successful.

"; } } /** * Simple event handler that displays that the form is valid. * * @access public * @param object patForms form object * @param string event name * @return mixed any value your function should return. You will get the value as a return value of patForms::renderForm() * @see patForms::registerEventHandler() */ function displaySuccess( &$form, $event ) { echo "patForms: validation successful.

"; } // create the needed renderer $renderer =& patForms::createRenderer( "Array" ); // set the renderer $form->setRenderer( $renderer ); // use auto-validation $form->setAutoValidate( 'save' ); // serialize the elements $elements = $form->renderForm(); // DISPLAY FORM ------------------------------------------------------ displayForm( $form, $elements ); // see patExampleGen/customFunctions.php // EXAMPLE END ------------------------------------------------------ $exampleGen->displayFooter(); ?>