* @link http://www.php-tools.net */ /** * Takes a patForms object, asks it if there are any validation * errors and displays them if need be. * * NOTE: this is just a helper method for our examples collection, * so that you may concentrate on the relevant parts of the examples. * It does in no way represent the way it should be done :) * * @access public * @param object &$form The patForms object to use */ function displayErrors( &$form ) { // get the errors from the form object - if there are none, // this returns false so it is easy to check if there are any. $errors = $form->getValidationErrors(); // if there are any errors, display them. if ($errors) { echo '
'; echo '
Validation failed
'; echo '
'; // the errors collection is an associative array with the // field names as keys, so we go through that. foreach ($errors as $elementName => $elementErrors) { $element =& $form->getElementByName( $elementName ); if (is_array($element)) { $element = $element[0]; } if (!is_object($element)) { patErrorManager::raiseError("patExampleGen::0001", "The getElementByName() method did not return an element."); } // each element can have more than one error - this // is rare, but can happen so this is an indexed array // with one error in each row. foreach ($elementErrors as $row => $error) { echo '
'; echo ' '.$element->getAttribute( 'label' ).': '.$error['message'].' ('.$error['element'].' element error #'.$error['code'].')
'; echo '
'; } } echo '
'; echo '
'; } // no errors, tell the world everything is fine else { echo '
Validation successful.
'; } } /** * Displays a standard form from the examples collection when the * form is rendered via the array renderer. Does not work for any * other examples. * * NOTE: this is just a helper method for our examples collection, * so that you may concentrate on the relevant parts of the examples. * It does in no way represent the way it should be done :) * * @access public * @param object &$form The current form object * @param array $elements The rendered elements from the * @return * @see */ function displayForm( &$form, $elements ) { // output the opening form tag echo $form->serializeStart(); // display all elements foreach( $elements as $element ) { echo '
'; echo $element['label']."
"; echo "
".$element["element"]."
"; if( isset( $element["description"] ) ) { echo "".$element["description"]."
"; } echo '
'; } // submit button, closing form tag echo '

'; echo $form->serializeEnd(); // form submitted? display all form values if( $form->isSubmitted() ) { $els =& $form->getElements(); $cnt = count( $els ); echo '
'; echo '
Submitted form values
'; echo '
'; echo ' '; for( $i = 0; $i < $cnt; $i++ ) { echo ''; echo ' '; echo ''; } echo '
'.$els[$i]->getAttribute( 'label' ).' : '.var2string( $els[$i]->getValue() ).'
'; echo '
'; echo '
'; } } function var2string( $subject ) { if( is_object( $subject ) ) { $class = get_class( $subject ); return 'object "'.$class.'"'; } if( is_int( $subject ) ) { return 'int '.$subject.''; } if( is_null( $subject ) ) { return 'null'; } if( is_array( $subject ) ) { $string = ''; foreach( $subject as $key => $val ) { $string .= str_repeat( ' ', 6 ).$key.' => '.var2string( $val ).'
'; } return 'array(
'.$string.');
'; } if( is_bool( $subject ) ) { $display = 'true'; if( $bool === false ) { $display = 'false'; } return 'bool '.$display.''; } return ''.gettype( $subject ).' "'.$subject.'"'; } ?>